Search code examples
ruby-on-railsglobalize

globalize with search_cop - unknown attribute


I'm trying to use the globalize gem and search_cop together. In my model I have:

class Museum < ApplicationRecord
  include SearchCop

  has_one_attached :hero_image
  translates :name, :address, :description, :facilities, :hours, :tickets

  search_scope :search do
    attributes :name, :address
    options :name, :type => :fulltext
    options :address, :type => :fulltext
  end
end

But when I go to search I get:

irb(main):006:0> Museum.search("art")
SearchCop::UnknownAttribute: Unknown attribute museums.name

Is it possible to use Globalize and SearchCop together? if so, how do I specify the translated fields to search on?


Solution

  • To use Globalize with SearchCop you need to define the translated attributes through their association. So something like:

    search_scope :search do
      attributes name: "translations.name", address: "translations.address"
      options :name, :type => :fulltext
      options :address, :type => :fulltext
    end