I have 4 models: Artists, Editions, Events & EventItems
.
- Editions have many Events
- Events have many Artists through EventItems
- Editions have many Artists through Events
On artists I have a scope published
I define active as an artists who is on an event which has a state which is NOT draft
.
scope :published, -> { joins(:events).where("events.state != ?", 'draft').uniq }
This works fine until I start chaining them and want Artists who have at least on event which is published
for a given edition.
Edition.first.artists.published
This works except that it will also joins events
outside the current edition and wrongly publish artists if they have any events published (even if they are not in this particular edition).
To get it to work properly I had to hack it like so (which is horrible):
scope :published, ->(edition = nil) {
if edition.present?
joins(:events).where("events.state != ? AND events.edition_id = ?", 'draft', edition.id).uniq
else
joins(:events).where("events.state != ?", 'draft').uniq
end
}
Edition.first.artists.published(Edition.first)
Is there anyway to give more context to the scope to only include those events in this edition? So it will scope correctly?
Thanks!