Request for multiple Rails 3 requests

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 

In addition, I have a full-text search (from gem) for the item model, I can do Item.search ('keyword') - โ€œsearchโ€ is the area provided by the gem to get all items with a keyword for description or description , the rank attribute has been added to the result element for relevance to the match

I also have a Geo search (from the gem) for a location model, I can do Location.near ('Toronto. ON', 100) --- "nearby" is the area provided by the gem to get all locations in 100 km from Toronto, in the place of the result the attribute "distance" is added for the distance from this location - Toronto in this example

So now I am trying to get a list of location_item objects that have a location corresponding to a specific location and an element matching a given keyword. For example, find location_item objects matching the โ€œkeywordโ€ and within 100 km of Toronto.

How can I achieve this with query one ? and can also access distance and rank attributes through an associated element and location inside the location_item object.

It seems like I canโ€™t focus the scope since they only work with Item and Location, not with LocationItem,

For example, the expression below will not work

LocationItem.joins (: elements, locations) .search ('keyword'). near ('Toronto, ON', 100)

Hopefully my description of what I'm trying to do makes sense. Do you have any ideas? Thank you very much!

+4
source share
1 answer

Basically, you cannot do whatever you want in a single request. Keep the usual LocationItem # location and LocationItem # you are looking for. I assume that you are using Postgres full-text search because the question doesn't make sense * if you are using Solr or Sphinx **.

If you need one request, but do not mind abandoning the belongs_to interface of the returned elements: ActiveRecord :: Base automatically assigns attributes from any part of the SELECT request, if provided, for example: Location.select('id+1 as more_id').first.more_id #=> 2 so that you can take advantage of this and create attributes that have the corresponding parts of the location, item and item_rank and location_dist.

 class LocationItem < ActiveRecord::Base #This presumes that your geo and search gems actually return scopes that # properly respond to to_sql (which they should if you're using rails 3). def self.local_matching_items(text, dist=100) LocationItem .joins("INNER JOIN #{Item.search(text).to_sql} as matching_items on matching_items.id = location_items.item_id") .joins("INNER JOIN #{Location.near(dist).to_sql} as nearby_locations on nearby_locations.id = location_items.location_id") .select("location_items.id, nearby_locations.distance, matching_items.rank, nearby_locations.other_field_you_might_want as location_other_field_you_might_want, matching_items.name as item_name, etc") #returns LocationItems with #id, #distance, #rank, # location_other_field_you_might_want, #item_name, etc #It might be most helpful to distinguish between these # and normal location_item by storing them in variables with a # different naming convention like nearby_item_matches. end end 

* Because then you will make two requests, one for matching keywords through your search provider, one for getting your records from db.

** If you are using ThinkingSphinx, which already has support for searching by geographic distance , but you need to define your index in different ways and call LocationItem # search in different ways.

+2
source

All Articles