We've scrapped the validations on Address in the project as it wasn't
important to the biz model.  Here's some sudo-code (good pun?)

#
Account--------------------------------------------------------------
class Account < ActiveRecord::Base
  has_one :address, :as => :addressable, :dependent => :destroy
  accepts_nested_attributes_for :address
end

# Address
--------------------------------------------------------------
class Address < ActiveRecord::Base
  belongs_to  :addressable, :polymorphic => true

 
validates_presence_of :street_address, :city, :state, :postal_code, :if
=> :should_validate?

  def should_validate?
    # Admittedly lame, but trying not to upset an existing house of
cards.
    # Here the Account fields have not been updated yet when updating,
but are set on a new instance of Account.
    # If validations are removed from this class (Address), the
address doesn't fail and then the Account records updates and saves
    # as normal.
    addressable.respond_to?(:is_admin_account) ?
addressable.is_admin_account? : false
  end
end

So, what it seems from my observation is that the Address model gets
validated before the Account fields are set.  I checked and this is
true for all fields, not just the one that I'm checking.  It seems
kind of odd for this to be a default behavior, no?

Your solution, below, is what I was trying to work around as I wasn't
able to get a hold of the client and was trying to leave things as I
found them.  Like I was telling Marnen, this isn't my design; I've
been hired in to work on an existing application.  Woe be to he that
wrote this thing if I ever catch him in a dark alley -- you should see
what I have to work with!

Because a language allows you to do things doesn't make them right =)

On Dec 4, 2:19 am, Michael Pavling <[email protected]> wrote:
> On 3 December 2010 20:51, Ray Parker <[email protected]> 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....

-- 
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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to