2016-07-24 106 views
0

我正嘗試在事件模型上創建全文搜索。 該模型包含country_code字段,其中存儲國家代碼; 我希望能夠按國家直接輸入國家名稱,比方說法國而不是法國。使用虛擬屬性搜索

我的範圍是這樣的:

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

我有一個國家的方法

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

不幸的是叫我的搜索範圍產生以下錯誤:

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

如何解決這個?

回答

0

您是否正確運行了遷移?看起來你的數據庫甚至沒有國家字段,所以難怪它無法搜索它。

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

您也可能沒有在模型中正確鏈接表格。很難說沒有所有的細節,但這看起來很腥column events.country

事件應該是單數,國家應該是一個像column event.country_id這樣的ID字段。也許你沒有在模型中正確加入表格,或者你的遷移可能缺少正確的ID字段。

# models/events.rb 
has_one :country 

# models/country.rb 
belongs_to :event 

如果這不是答案,我們需要更多有關您的國家和事件遷移的信息。

+0

感謝您的回覆。 我的活動模型沒有country_code字段的國家/地區字段。 我正在使用gem country_select,因此我不需要存儲完整的國家/地區名稱,也不必爲國家/地區提供單獨的表格。這就是爲什麼我只有一個國家的contry_code字段。 國家方法的作用是返回給定country_code的國家名稱 – user1445685