lundi 14 avril 2014

Trying to access sphinx internal data


Vote count:

0




I have a table for countries and provinces. The models for each are listed below. I am adding thinking sphinx as a search and would like to use it to get the results displayed.


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


In my view I need to add a conditional link to the provinces which belong to a certain country if the province count for that country is greater than 0.



count = country.provinces.count
if count > 0
link_to(country.country_name, provinces_path(:id => country.id))
else
country.country_name
end


I tried to replace the active record query for count with



count = Country.search(:select => "province_count", :with => {:country_name => country.country_name})


but am not yet successful in getting this to work yet. How can this be achieved. I was working off of this link



asked 17 secs ago






Aucun commentaire:

Enregistrer un commentaire