I’m saving an invalid User record through an Account record during a registration wizard. I have methods that let me conditionally skip validating certain User attributes. When I check validity of the data through the association it shows as valid, when I check validity on it’s own, it shows invalid.
ex:
$ account = Account.new
$ account.users.new(:email => nil)
$ account.save
=> true
$ account.users.last.valid?
=> true
$ User.last == account.users.last
=> true
$ User.last.valid?
=> false
Any ideas?
They don’t do the same thing, since one is using memory and the other is using the database.
From the account object, take the users association (in memory), get the last element, check if valid.
Compare, primary key (:id field) of last user in database and last user in account.users association (in memory), and return true if the primary key is the same.
Check if the last user in the database is valid.
Try the following:
The true argument reloads the users association from the database.