rubyonrailsin

A Ruby and Rails talk

Thursday, April 1, 2010


[Rails] ActiveRecord::Errors is empty when the record is invalid

by rubyonrailsin 0 comments

Tag


Share this post:
Design Float
StumbleUpon
Reddit

I'm running into an interesting problem where an ActiveRecord instance
#valid? returns false, but #errors#full_messages is empty.

This is in one of my models. The code was run in ruby-debug sitting in
one of my validation methods.
(rdb:1) self.publisher_address.valid?
false
(rdb:1) self.publisher_address.errors.full_messages
[]
(rdb:1) a = Address.new(self.publisher_address.attributes)
(rdb:1) a.valid?
false
(rdb:1) a.errors.full_messages
["User can't be blank", "City can't be blank"...]

self.publisher_address is an unsaved Address object, created using
Address.new. So is a, which I just created. Both are invalid, yet a
has error messages like it should but self.publisher_address doesn't.
I can't figure out why.

(rdb:1) self.publisher_address
#<Address id: nil, user_id: nil,...>
(rdb:1) a
#<Address id: nil, user_id: nil,...>
(rdb:1) self.publisher_address.class
Address(id: integer, user_id: integer,...)
(rdb:1) a.class
Address(id: integer, user_id: integer,...)

The #publisher_address method in this model is custom, not an
association. I get the hash of the attributes and do
return Address.new(publisher_address_attributes)

self.publisher_address.errors is an AR::Errors object, but the actual
errors are blank:
(rdb:1) self.publisher_address.errors
#<ActiveRecord::Errors:0x2cddf50 @base=#<Address id: nil, user_id:
nil,...>, @errors=#<OrderedHash {}>>

Has anyone seen empty errors like this before?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment

Subscribe feeds via e-mail

Blog Archive