1

I have an Invites table in my database and when i'm perform a task as a user I want to pass an Event.id to a scope User.not_invited_to(event)

How do I build this scope to check if the user ID is NOT in there?

class User
  has_many :invites, :foreign_key => 'participant_id'
  ...
end

class Invite
  belongs_to :participant, :class_name => "User", :foreign_key => "participant_id"
  ...
end
Joseph Le Brech
  • 6,541
  • 11
  • 49
  • 84

1 Answers1

0

You can use event.invite_ids:

scope :not_invited, lambda { |event| where("id not in (?)", event.invite_ids) }
Mischa
  • 42,876
  • 8
  • 99
  • 111
  • I did not realise this, i've being using collect a lot. so would .._ids do that for me? – Joseph Le Brech Apr 04 '13 at 13:47
  • Yes, `*_ids` does that for you. It's [one of the things added](http://edgeguides.rubyonrails.org/association_basics.html#has-many-association-reference) to your ActiveRecord object when it has a `has_many` association to an other model. – Mischa Apr 04 '13 at 13:58