.where-query on "grandparent" in nested models in Rails -
is possible follwing query, without 2 models having direct association:
event.where(community_id: 1)
these 3 models:
class community < activerecord::base has_many :organizers end class organizer < activerecord::base belongs_to :community has_many :events end class event < activerecord::base belongs_to :organizer end
the closest i've come use "delegate" following, works event.communities not query:
delegate :community, :to => :organizer, :allow_nil => true
you can this:
event.joins(organizer: :community).where("communities.id = ?", 1)
or:
event.joins(:organizer).where("organizers.community_id = ?", 1)
Comments
Post a Comment