Forum: Ruby on Rails Can not figure out why validation fails

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C608d449f87ba87dd4a3f653afb304c1?d=identicon&s=25 Evan (Guest)
on 2008-10-21 09:09
(Received via mailing list)
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
9b4c04c050122bcea16a6f3376d680fa?d=identicon&s=25 Daniel Bush (danb)
on 2008-10-21 10:17
(Received via mailing list)
On Oct 21, 6:08 pm, Evan <evancha...@gmail.com> 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/Va...
"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
9b4c04c050122bcea16a6f3376d680fa?d=identicon&s=25 Daniel Bush (danb)
on 2008-10-21 10:24
(Received via mailing list)
On Oct 21, 7:16 pm, Daniel <dlb.id...@gmail.com> 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 Bush

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 Bush
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2008-10-21 13:27
(Received via mailing list)
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
C608d449f87ba87dd4a3f653afb304c1?d=identicon&s=25 Evan (Guest)
on 2008-10-22 05:41
(Received via mailing list)
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
9b4c04c050122bcea16a6f3376d680fa?d=identicon&s=25 Daniel Bush (danb)
on 2008-10-22 06:41
I'm getting an error when posting thru google; this is 3rd time,
sorry...

On Oct 22, 2:40 pm, Evan <evancha...@gmail.com> 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 Bush
9b4c04c050122bcea16a6f3376d680fa?d=identicon&s=25 Daniel Bush (danb)
on 2008-10-22 06:52
(Received via mailing list)
On Oct 22, 2:40 pm, Evan <evancha...@gmail.com> 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 Bush
9b4c04c050122bcea16a6f3376d680fa?d=identicon&s=25 Daniel Bush (danb)
on 2008-10-22 06:53
(Received via mailing list)
On Oct 22, 2:40 pm, Evan <evancha...@gmail.com> 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 Bush
C608d449f87ba87dd4a3f653afb304c1?d=identicon&s=25 Evan (Guest)
on 2008-10-22 07:18
(Received via mailing list)
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
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2008-10-22 21:22
(Received via mailing list)
Daniel wrote:
> 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.

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
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2008-10-22 21:25
(Received via mailing list)
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:

http://github.com/rails/rails/tree/master/activere...

--
Rails Wheels - Find Plugins, List & Sell Plugins -
http://railswheels.com
C608d449f87ba87dd4a3f653afb304c1?d=identicon&s=25 Evan Chaney (Guest)
on 2008-10-23 03:48
(Received via mailing list)
Thanks, Mark.
This topic is locked and can not be replied to.