I have the following polymorphic associations:
class Invite < ActiveRecord::Base
belongs_to :inviteable, :polymorphic => true
class Event < ActiveRecord::Base
has_many :invites, :as => :inviteable, :dependent => :destroy
class Team < ActiveRecord::Base
has_many :invites, :as => :inviteable, :dependent => :destroy
I’m trying to create scopes on Invites specific to events, and to future events:
(in Invite.rb)
scope :to_events, where('inviteable_type = ?', "Event")
scope :to_future_events, lambda { self.to_events.joins("join events").where('events.starttime > ?', Time.now) }
The to_events scope works fine; the to_future_events returns the correct subset of invites, but it returns many copies of each. Any idea why? Is there a cleaner/Rails-ier way to accomplish what i’m trying to accomplish?
You have to provide an ON statement for the join:
Otherwise it joins every invite record with every events record.