On Oct 21, 6:08 pm, Evan <[EMAIL PROTECTED]> wrote:
> Good morning.
>
> I can not for the life of me figure out why validation of objects of
> one of my classes always fails.
>
> http://pastebin.com/m754b5bee
>
> The highlighted lines are where I am getting errors. The unit test
> fails on the highlighted line every time. If I
> remove :olive_oil_sources from validates_presence_of validation works
> fine.
>
> I have also tried writing my own validate method:
>
> if ( olive_oil_destination.nil? || olive_oil_sources.empty? )
> errors.add_to_base("blah")
> end
>
> which also fails even though each individual piece of the if
> expression evaluates true when run under console.
>
> Why is this object not validating? Driving me nuts.
>
> Thanks.
>
> Evan
Look at the documentation for this validation:
http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M001336
"If you validate the presence of the associated object, you will get
failures on saves when both the parent object and the child object are
new."
o.olive_oil_sources << os
will not save 'os' because 'o' is a new record.
I don't know if this is a chicken or egg thing you've got going here
or not. You might have to enforce the required associations for
OliveOil a different way.
Have your controller or model generate all the relevant objects; maybe
save them in a transaction.
Then you have to test for situations where all the olive oil sources
for an olive_oil are deleted to ensure that this type of situation
can't occur (or rethink this requirement). etc etc -
You could probably do this with a validation because I think you can
stipulate when the validation occurs eg at creation time, at update
time etc So rewrite the validation to take effect only when updating.
I would do all this testing in the models (test/units) myself.
--
Daniel Bush
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---