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