# Validates\_associated doesn't work on update (and i've tried :on =\> :update)

**URL:** <https://www.ruby-forum.com/t/validates-associated-doesnt-work-on-update-and-ive-tried-on-update/140106>\
**Category:** Rails\
**Created:** [June 10, 2008, 11:27pm UTC](https://www.ruby-forum.com/t/validates-associated-doesnt-work-on-update-and-ive-tried-on-update/140106 "2008-06-10T23:27:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [June 10, 2008, 11:27pm UTC](https://www.ruby-forum.com/t/validates-associated-doesnt-work-on-update-and-ive-tried-on-update/140106/1 "2008-06-10T23:27:57Z")

</div>

HI all-

I have two models, Story and Tag, and they are habtm. My goal is to  
show the errors for @story in a form on both new and edit. Everything  
works fine on new/create, but it fails on edit/update. First the  
models:

class Story \< ActiveRecord::Base  
attr\_accessor :tag\_string\_holder #holds a CSV of tags  
…  
has\_and\_belongs\_to\_many :tags, :uniq=\> true  
…  
def validate\_associated\_records\_for\_tags() end #to remove the  
generic “is invalid” error for tags  
…  
validates\_associated :tags, :message =\> ‘may consist of only  
letters, numbers, and underscores’  
…

class Tag \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :stories, :uniq =\> true, :order =\>  
“stories.created\_at DESC”  
…  
validates\_presence\_of :tag  
validates\_format\_of :tag, :with =\> /^[A-Za-z0-9\_]+$/, :message =\>  
‘Tags may consist of only letters, numbers, and underscores’

Ok, so here’s the stories controller create method:

def create #what you do when the new form is submitted  
@story = Story.new(params[:story])  
@story.user\_id = current\_user.id  
if request.post?  
begin  
Story.transaction do  
@story.write\_tags  
@story.save!  
flash[:notice] = ‘Story posted!’  
redirect\_to :action =\> ‘index’  
end  
rescue ActiveRecord::RecordInvalid =\> e  
render :action =\> “new”  
end  
…

Now, this works fine, I see the correct error on the stories form  
(from errors\_for :story) if I put an invalid tag in.

Here’s the update method:

def update  
@story = current\_user.stories.find(params[:story][:id])  
@story.attributes = params[:story]  
if request.post?  
begin  
Story.transaction do  
@story.write\_tags  
@story.save!  
flash[:notice] = ‘Story updated!’  
redirect\_to :action =\> ‘index’  
end  
rescue ActiveRecord::RecordInvalid =\> e  
@story.valid?  
render :action =\> “edit”  
end  
…

Now, I get no such error. I get nothing. If I go back and change the  
validates\_associated to:

validates\_associated :tags, :on =\> :update, :message =\> ‘may consist  
of only letters, numbers, and underscores’

Still nothing.

In the console, if I mimic this behavior, by loading an existing  
story, creating a new tag, setting its tag to something invalid, when  
I add that tag to the story:

story.tags \<\< tag (where tag is invalid)

An exception is thrown. After the exception, story has no errors, and  
tag does have an error.

So the question is, how do you use validates\_associated with an  
already saved master on update?

Thanks for any help,  
Dino

---

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [June 11, 2008, 8:21am UTC](https://www.ruby-forum.com/t/validates-associated-doesnt-work-on-update-and-ive-tried-on-update/140106/2 "2008-06-11T08:21:18Z")

</div>

Dino,

I would recommend looking at the write\_tags method in story. The  
create and update cases are very different here because during create  
the tags can’t be persisted until the story is saved and an id is  
generated. When a story is being updated its possible the tags are  
being saved and removed from memory before the story is saved. In  
that case the story validation would never encounter the invalid tags.

One other thing to check. Are the invalid tags actually being  
persisted or not? If they are then something is wrong with the  
validation.

Aaron

---

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [June 11, 2008, 2:51pm UTC](https://www.ruby-forum.com/t/validates-associated-doesnt-work-on-update-and-ive-tried-on-update/140106/3 "2008-06-11T14:51:04Z")

</div>

Aaron B. wrote:

> Dino,
> 
> I would recommend looking at the write\_tags method in story. The  
> create and update cases are very different here because during create  
> the tags can’t be persisted until the story is saved and an id is  
> generated. When a story is being updated its possible the tags are  
> being saved and removed from memory before the story is saved. In  
> that case the story validation would never encounter the invalid tags.

Thanks Aaron, this is exactly what was happening. I wound up using  
build to create new tags and just associating existing tags, thus  
allowing the story validation to be reached in the event of an invalid  
tag. Thanks so much for your response.

Dino
