Validation Example Wrong

29 views
Skip to first unread message

Bala Paranj

unread,
Mar 28, 2016, 3:15:29 PM3/28/16
to Ruby on Rails: Documentation
class Person < ApplicationRecord
  validates :name, presence: true
end
 
>> p = Person.new
# => #<Person id: nil, name: nil>
>> p.errors.messages THIS IS WRONG
# => {}                 THIS IS WRONG
 
>> p.valid?
# => false
>> p.errors.messages
# => {name:["can't be blank"]}
 

Vipul A M

unread,
Mar 28, 2016, 3:20:41 PM3/28/16
to rubyonra...@googlegroups.com
Hi Bala, its working as expected. Until you call `valid?` or `save`,
no validations are run, and errors hash would be blank.
What would be the expected the output to be here, for you?

Thanks,
Vipul A.M.
+91-8149-204995
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Documentation" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-do...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> Visit this group at https://groups.google.com/group/rubyonrails-docs.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages