How to combine these two Queries in Rails 3? -
in view, need show list name of each city available services in area (city services) below each name. involves 2 queries. but, how can combine them , show simple list?
the models...
class city < activerecord::base has_many :city_services, :dependent => :delete_all end class cityservice < activerecord::base belongs_to :city end
the controller... need help.
@county = params[:county] @city_ids = city.where("county = ?", @county).map { |c| c.id } @city_services = cityservice.where( :city_id => @city_ids )
but, there multiple services each city, can't list city_services, or cities listed multiple times.
try (i assume cities table called cities):
@city_services = cityservice.joins(:city).where(cities: {country: params[:country]})
if want distinct list:
@city_services = cityservice.joins(:city).where(cities: {country: params[:country]}).uniq
Comments
Post a Comment