sql - rails scope for find children count in has_many -
in ruby on rails application have 2 models:
class curatorgroup < activerecord::base has_and_belongs_to_many :art_objects, join_table: "curator_groups_art_objects" end class artobject < activerecord::base scope :currently_displayed, -> { where(status: "currently on display") } has_and_belongs_to_many :curator_groups, join_table: "curator_groups_art_objects" end
no want write scope returns me curator groups have @ least 1 art object? how can this?
this should work. have tried similar association on own project. i'm hoping i've translated project without typos.
curatorgroup.joins('left outer join curator_groups_art_objects on curator_groups_art_objects.curator_group_id = curator_groups.id').group('curator_groups.id').having('count(curator_groups_art_objects.curator_group_id) > 0').all
Comments
Post a Comment