activerecord - How to retrieve all related objects from an existing selection set in Rails -
in rails app have 2 models related via has_many
, belongs_to
associations:
class entity < activerecord::base has_many :applicants end class applicant < activerecord::base belongs_to :entity end
i'm using searchkick first select entities - details of don't matter, given existing collection of entity objects, how can retrieve of related applicant objects?
# select entities (in app i'm using searchkick run search) entities = entity.search params[:query] # find applicants related selected entities applicants = entities.???
this works, slow given large selection:
# retrieve applicants related selected entities applicants = [] entities.each |entity| applicants += entity.applicants end
is there rails short-hand retrieve applicants related selected entities?
other things i've tried:
entities.class => entity::activerecord_relation entities.applicants => #error entities.applicant_ids => #error
like this?
applicant.joins(:entity).where("entities.id < ?", 1000)
Comments
Post a Comment