ruby - rails, how to combine two ActiveRecord query results -
i have following association
class location < activerecord::base has_many :items end class item < activerecord::base belongs_to :location end
suppose have instances of location, query items belong locations. managed result array
items =[] location.near(latitude,longitude,distance).find_each |location| location.items.find_each |item| items << item end end
however, there way can results activerecord::relation. because want further query results using "where" activerecord::relation.
p.s. "near" method geocoder gem, returns activerecord::relation.
---------------------edit----------------------------
thank replies find solution
locations = location.near(latitude,longitude,distance) item.where(location_id: locations.pluck(:id))
is right way it? me bit unintuitive.
----------------------edit again ---------------------------
just small comment: unintuitive because switching datamapper. if datamapper, quite simple, like
location.near(blabla).items
it make queries through associations. compared datamapper, can not understand why activerecord association useless?
edit use 1 query mapping...
what billy said above, option might faster:
locations = location.near(1, 2, 3) items = item.where(:location_id => locations.map(&:ids)
Comments
Post a Comment