Friday, December 3, 2010

Re: [Rails] Order of Validation for Associated Models

On 3 December 2010 20:51, Ray Parker <rayparkerbassplayer@gmail.com> wrote:
> Where I'm running into issue is that the Address model validates
> before the values are updated in the Account model and, based on the
> bool value, I want to validate or not.

I'm not sure what you mean by "the Address model validates before the
values are updated in the Account model" - can you extract some model
code to illustrate?

> Anyone have a good place for me to start?

It occurs to me, that if an Address can be different depending on the
model it's associated with, the validation check needs to move to the
associated model maybe. How about adding something like this to
Account:

validates :valid_address if :address

private
def valid_address
errors.add_to_base "address is not valid for one reason or
another" unless address.valid_for_account
end

... and this to Address:

def valid_for_account
return true if self.post_code.nil?
return true if self.some_other_validation_check_returns_true
return true if self.etc
end

You could merge the Errors from Address into Account, rather than just
adding a generic one to base, to display "nice" messages to the user.
http://dev.rubyonrails.org/attachment/ticket/11394/merge_bang_errors.patch

--
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.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate