Reputation: 8604
This is my model:
class User < ActiveRecord::Base
attr_accessible :email, :name
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: true
end
The author of Rails Tutorial Example said "Curly braces are optional when passing hashes as the final argument in a method", but here the presence
validation is not final argument, but it can be used without curly braces and is valid code. The format
validation of email attribute also works.
Anybody can explain me why?
Upvotes: 1
Views: 1383
Reputation: 27374
:name, :presence: true, length: { maximum: 50 }
is the last argument passed to validates
, so you don't need curly braces for it.
A case where you would need curly braces would be if you were passing arguments after that hash:
validates { :name, presence: true, length: { maximum: 50 } }, some_other_argument
Where some_other_argument
here is some hypothetical argument that comes after the hash. To process this correctly you would need the curly braces around the hash.
Upvotes: 2