Charlon
Charlon

Reputation: 363

good syntax for ruby scope

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|)

Upvotes: 0

Views: 54

Answers (1)

Igor Guzak
Igor Guzak

Reputation: 2165

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

Upvotes: 1

Related Questions