My application models a writing competition, with Users, Stories, Competitions and Submissions.
Users can participate in competitions through submissions, or as owners. I’d like one query or method that returns all competitions owned or participated in by a user. I’ve got a method returning those as an array, competitions | owned_competitions, but I’d really like to keep them ActiveRecord entities so I can chain, apply scopes, etc.
Is there a way to do that, or a better way to setup these relationships?
My models look like this:
user.rb:
has_many :stories
has_many :submissions
has_many :competitions, :through => :submissions
has_many :owned_competitions, :class_name => "Competition"
story.rb:
belongs_to :user
has_many :submissions
has_many :competitions, :through => :submissions
competition.rb:
belongs_to :owner, :class_name => "User", :foreign_key => 'user_id'
has_many :submissions
has_many :competitors, :through => :submissions, :source => :user
has_many :stories, :through => :submissions
scope :expiring_today, lambda { where("deadline = ?", Date.today) }
submission.rb:
belongs_to :user
belongs_to :story
belongs_to :competition
What you want is absolutely possible. Actually, it’s probably possible without even writing any SQL, but I don’t know precisely how.
Here’s what I’ve come up with:
We use a left join (rather than the default inner join) so that we can still return competitions that a user owns but which have no submissions yet.