update_attributes and validations

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?

Right now, this is happening:

me = Person.find(1)

=> #<Applicant:0x31e2d0c @attributes={"first_name"=>"Julia", ... }>

me.lab_urls

=> [#<LabUrl:0x31dfc4c @attributes={"title"=>"My Lab Webpage", "url"=>"http://mylabissuper.edu/labpage/&quot;, "id"=>"1", "person_id"=>"1"}>]

me.lab_urls.find(1).update_attributes({"title"=>"", "url"=>"http://mylabissuper.edu/labpage/&quot;, "id"=>"1", "person_id"=>"1"})

=> false

me.valid?

=> true

Any thoughts?

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.

Thanks, Julia

You can add behavior to Active Record association by using a block. In your case, it will be something like:

Person   has_many :lab_urls do

    def make_url_invalid       find(1).blah_blah_blah       # Here you also need to update the person record invalid     end   end end

I have not run this code. It is based on Rails Recipes book, if it doesn't work blame Chad :wink:

You can add behavior to Active Record association by using a block. In your case, it will be something like:

Person   has_many :lab_urls do

    def make_url_invalid       find(1).blah_blah_blah       # Here you also need to update the person record invalid     end   end end

I have not run this code. It is based on Rails Recipes book, if it doesn't work blame Chad :wink:

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?

Right now, this is happening:

me = Person.find(1)       

=> #<Applicant:0x31e2d0c @attributes={"first_name"=>"Julia", ... }>   

me.lab_urls       

=> [#<LabUrl:0x31dfc4c @attributes={"title"=>"My Lab Webpage", "url"=>"http://mylabissuper.edu/labpage/&quot;, "id"=>"1", "person_id"=>"1"}>]   

me.lab_urls.find(1).update_attributes({"title"=>"", "url"=>"http://mylabissuper.edu/labpage/&quot;, "id"=>"1", "person_id"=>"1"})       

=> false   

me.valid?       

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

Lionel.