jared_flack
jared_flack

Reputation: 1646

Ruby / Rails syntax

How does this syntax work?

before_validation { |user| user.email = email.downcase }

I would think that it would need to be this:

before_validation { |user| user.email = user.email.downcase }

Thanks for your help!

Upvotes: 4

Views: 153

Answers (2)

Aditya Kapoor
Aditya Kapoor

Reputation: 1570

It works, but keep the DRY principle of Ruby. This would be better:

before_validation { |user| user.email.downcase! }

! reflects the changes back onto the receiving object and it also saves a few keystrokes.

Upvotes: -1

Salil
Salil

Reputation: 47472

It work because

before_validation { |user| user.email = email.downcase }

SAME AS

before_validation { |user| user.email = self.email.downcase }

Upvotes: 5

Related Questions