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

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -