0

I just wanna know if there is a way of doing this :

I want an "invited" method for my user class to create a list of user(s) that have their referral_code equals to the self.confirmation_token

I tried a lot of things and the last thing was almost good I know it, but I have a syntax error ...

scope :invited, -> {|u| where("referral_code = ?", confirmation_token)}

ofc by this I mean that I want to iterate on every user in the database (|u|)

Charlon
  • 363
  • 2
  • 16

1 Answers1

1

you could write:

scope :invited, ->(token) { where("referral_code = ?", token) }

and then: User.ivited(some_token), but if you need users who have the same referral_code and confirmation_token fields, you could write:

scope :invited, -> { where "referral_code = confirmation_token" }



according to your comment (I need users who have the same referral_code than the confirmation_token of the user caller of the method invited), you could write:

def invited
  where "referral_code = ?", confirmation_token
end

and then: User.last.invited

Igor Guzak
  • 2,155
  • 1
  • 13
  • 20
  • 1
    ["Using a class method is the preferred way to accept arguments for scopes."](http://guides.rubyonrails.org/active_record_querying.html#passing-in-arguments) so `def self.invited(token) ...` would be the recommended option. – mu is too short Sep 09 '14 at 17:27
  • Thank you ! But I need users who have the same referral_code than the confirmation_token of the user caller of the method invited – Charlon Sep 09 '14 at 21:48