我有一個國家的表格(下面列出的模型)。我將思維獅身人面像添加爲搜索,並希望用它來顯示結果。試圖訪問獅身人面像內部數據
country.rb
class Country < ActiveRecord::Base
has_many :provinces
has_many :cities
has_many :zones
has_many :users
attr_accessible :alpha_2, :alpha_3, :country_name, :numeric, :country_active
scope :ordered, order("country_name")
scope :active, where(:country_active => true)
end
country_index.rb
ThinkingSphinx::Index.define :country, :with => :active_record do
indexes country_name, :sortable => true
has created_at, updated_at
has "(select COUNT(provinces.id) from provinces where provinces.country_id = id)", :as => :province_count, :type => :integer
end
在我看來,我需要一個有條件的鏈接添加到屬於某一個國家的省份,如果全省計數該國大於0.
count = country.provinces.count
if count > 0
link_to(country.country_name, provinces_path(:id => country.id))
else
country.country_name
end
我試圖用count替換活動記錄查詢w ith
count = Country.search(:select => "province_count", :with => {:country_name => country.country_name})
但是我還沒有成功地完成這項工作。這怎麼能實現。我工作過的this link
非常感謝了詳細的解答。你搖滾! –