I have models below:
class Location < ActiveRecord::Base
has_many :location_items
has_many :items, :through=>:location_items
end
class Item < ActiveRecord::Base
has_many :location_items
has_many :locations, :through=>:location_items
end
class LocationItem < ActiveRecord::Base
belongs_to :item
belongs_to :location
end
Also I have a full text search (from gem) enabled for item model, I can do Item.search('keyword') -- 'search' is a scope provided by the gem to get all items with name or description matching keyword, the result item is added a 'rank' attribute for relevance of the match
I also have a Geo search (from gem) enabled for location model, I can do Location.near('Toronto. ON', 100) --- 'near' is a scope provided by the gem to get all locations within 100 km from Toronto, the result location is added a 'distance' attribute for distance from the given location - Toronto in this example
So now what I am trying to do is to get a list of location_item objects that have location match certain given location and item match given keyword. For example, search for location_item objects matching 'keyword' and within 100km of Toronto.
How can I achieve this with one query? and can also have access to distance and rank attributes through associated item and location inside location_item object.
I can't seem to chain the scope for they only work on Item and Location, not LocationItem,
For example, the expression below won't work
LocationItem.joins(:items, locations).search('keyword').near('Toronto, ON', 100)
Hope my description of what I am trying to do makes sense. Do you have any idea? Thank you very much!