ruby on rails - Mongoid: find all children documents of multiple parent documents -


i have following 2 models in rails 3.2.13 app:

class organization   include mongoid::document   include mongoid::search    field :name   field :description    has_many :locations    search_in :name, :description, :locations => [:name, :description, :keywords] end  class location   include mongoid::document    field :name   field :description   field :keywords, type: array    belongs_to :organization    def self.find_by_keyword(keyword)     locs = []     orgs = organization.full_text_search(keyword)     orgs.each { |org| locs.push(org.locations) }     locs.flatten   end end 

in locations_controller.rb, have search method:

def search   @results = kaminari.paginate_array(location.find_by_keyword(params[:keyword])).page(params[:page]).per(30) end 

using mongoid_search gem, can keyword (the search term) in fields both organization , location models, , organizations match:

orgs = organization.full_text_search(keyword)

but want return locations belong organizations search result. way able iterate through each organization, push locations array, return flattened array. in order controller code work, had use kaminari's paginate_array method.

my question is, there better way achieve same result without using paginate_array method?

thanks!

you can use organization_id.in

@locations = location.where(:organization_id.in => orgs.map(&:id)) 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -