I have a Person model. Person has_many lab_urls. The LabUrl model has
a title and a url, which are both required.
Now, I want to update a LabUrl object and stick some invalid data in
there. And I want the person that has that invalid LabUrl to now be
invalid, too. That doesn't seem like too much to ask for. How can I do
that?
I should clarify that I know the LabUrl isn't being saved, and thus
when I look at the lab_urls collection, the "updated" url has the
original attribute values and so the Person is valid.
I guess I would like to know a workaround for this so that an update
form for Person and its related lab_urls can catch that the updated
LabUrl is invalid, and then retain that invalid information and
associated error message, and then show that to the user to fix. Is
that possible without getting too hacky? This happens pretty
automagically when creating a new LabUrl with
@person.lab_urls.build(...), so it's sad that updating isn't as
graceful.
juliamae@gmail.com wrote the following on 26.02.2007 23:08 :
I have a Person model. Person has_many lab_urls. The LabUrl model has
a title and a url, which are both required.
Now, I want to update a LabUrl object and stick some invalid data in
there. And I want the person that has that invalid LabUrl to now be
invalid, too. That doesn't seem like too much to ask for. How can I do
that?
=> true
You did modify the first LabUrl returned by Person#lab_urls
If your Person valid state doesn't depend on its lab_urls valid state,
he's still valid.
You may want to add the following to Person:
validates_associated :lab_urls
Notes:
- update_attributes(... "id" => "1" ...) isn't doing anything, as you
can't change the id.
- me.lab_urls.find(1) seems suspicious to me. If you want to simulate an
invalid URL, me.lab_urls[0] should be more suitable.