Can not figure out why validation fails

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

On Oct 21, 6:08 pm, Evan [email protected] wrote:

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

On Oct 21, 7:16 pm, Daniel [email protected] wrote:

"If you validate the presence of the associated object, you will get
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 B.

whoa, what am i saying. Forget about that stuff to do with validating
on updates. Maybe a callback like before_destroy. I’ll best stop
here.


Daniel B.

Evan wrote:

fine.
Why is this object not validating? Driving me nuts.
For each has_many in a model, Rails adds an automatic validation
check of each child object.

So is an object created as OliveOilSource.new itself valid?


Rails Wheels - Find Plugins, List & Sell Plugins -
http://railswheels.com

Daniel,

I did not notice that warning about validates_presence_of before.
Thanks for bringing it to my attention. Your thought process helped me
understand better what is going on.

Evan

I’m getting an error when posting thru google; this is 3rd time,
sorry…

On Oct 22, 2:40 pm, Evan [email protected] wrote:

I did not notice that warning about validates_presence_of before.
Thanks for bringing it to my attention. Your thought process helped me
understand better what is going on.

Take a look at a more recent post from Mark R J:
http://www.ruby-forum.com/topic/168650
He shows how you can use validates_presence_of with assocations when
building a new object.
ie
u.books.build(:title => ‘The Comedy of Errors’, :author => u)
where u is an unsaved User object (used as an author). I can’t remember
your example exactly, but that might work for you with your current
validations. The way the (online) docs are worded, though, suggests
that the AR-people intended it to be used more for foreign key fields
than the associations based on them.
The callback before_destroy I mentioned was just a thought about how you
might stop an olive oil source from being ‘destroyed’ (‘delete’ won’t
invoke callbacks) if it happens to be the only source for an olive oil
object. Anyway, you have a bunch of validations and callbacks to use to
achieve whatever you need to do.


Daniel B.

On Oct 22, 2:40 pm, Evan [email protected] wrote:

Daniel,

I did not notice that warning about validates_presence_of before.
Thanks for bringing it to my attention. Your thought process helped me
understand better what is going on.

Evan

Take a look at a more recent post from Mark R J:
http://www.ruby-forum.com/topic/168650
He shows how you can use validates_presence_of with assocations when
building a new object.
ie
u.books.build(:title => ‘The Comedy of Errors’, :author => u)
where u is an unsaved User object (used as an author). I can’t
remember your example exactly, but that might work for you with your
current validations. The way the (online) docs are worded, though,
suggests that the AR-people intended it to be used more for foreign
key fields than the associations based on them.
The callback before_destroy I mentioned was just a thought about how
you might stop an olive oil source from being ‘destroyed’ (‘delete’
won’t invoke callbacks) if it happens to be the only source for an
olive oil object. Anyway, you have a bunch of validations and
callbacks to use to achieve whatever you need to do.


Daniel B.

Mark,

Thanks for the information about automatic validations in has_many-
related models. Can you point me to documentation on this? I’d like to
learn more about how it works.

Thanks.

Evan

On Oct 22, 2:40 pm, Evan [email protected] wrote:

Daniel,

I did not notice that warning about validates_presence_of before.
Thanks for bringing it to my attention. Your thought process helped me
understand better what is going on.

Evan

Take a look at a more recent post from Mark R J:
http://www.ruby-forum.com/topic/168650
He shows how you can use validates_presence_of with assocations when
building a new object.
ie
u.books.build(:title => ‘The Comedy of Errors’, :author => u)
where u is an unsaved User object (used as an author). I can’t
remember your example exactly, but that might work for you with your
current validations. The way the (online) docs are worded, though,
suggests that the AR-people intended it to be used more for foreign
key fields than the associations based on them.
The callback before_destroy I mentioned was just a thought about how
you might stop an olive oil source from being ‘destroyed’ (‘delete’
won’t invoke callbacks) if it happens to be the only source for an
olive oil object. Anyway, you have a bunch of validations and
callbacks to use to achieve whatever you need to do.


Daniel B.

Daniel wrote:

Take a look at a more recent post from Mark R J:
Trouble validating associated objects - Rails - Ruby-Forum
He shows how you can use validates_presence_of with assocations when
building a new object.

Thanks for that Daniel.

Yes, the warning in the API to validate the presence of the
foreign key rather than the belongs-to association itself is
wrong. It has now been removed.

However when the parent is new you have to assign the
belongs_to object manually, currently even when a new
has_many child is instantiated using the build method.


Rails Wheels - Find Plugins, List & Sell Plugins -
http://railswheels.com

Thanks, Mark.

Evan wrote:

Mark,

Thanks for the information about automatic validations in has_many-
related models. Can you point me to documentation on this? I’d like to
learn more about how it works.

Evan, it’s not currently documented in the API.

Check out the Rails source:


Rails Wheels - Find Plugins, List & Sell Plugins -
http://railswheels.com