0
votes

I'm trying to create full text search on an Event model. The model contains a country_code field where the country code is stored; I want to be able to search by country directly by typing the country name, let's say France instead of Fr.

My scope looks like this:

pg_search_scope :full_search, against: [:name, :city, :country], associated_against: {
    event_type: [:name],
    dance_types: [:name]
  }

I have a country method

def country
  return '' if country_code.blank?
  country = ISO3166::Country[country_code]
  (country.translations[I18n.locale.to_s] || country.name) unless country.nil?
end

Unfortunately calling my search scope produces the following error:

ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column events.country does not exist.

How can I fix this?

1

1 Answers

0
votes

Have you run your migrations properly? It appears your database doesn't even have a country field, so no wonder it isn't able to search it.

$ rake db:migrate VERSION=0
$ rake db:migrate

You also might not have linked the tables properly in your models. It's hard to tell without all of the details but this looks fishy column events.country

Events should be singular, and country should probably be an id field like column event.country_id. Maybe you haven't joined the tables properly in their models, or your migrations might be missing proper id fields.

# models/events.rb
has_one :country

# models/country.rb
belongs_to :event

If that isn't the answer, we need more information about your migrations for country and events.