# Saving a model with a through association

**URL:** <https://www.ruby-forum.com/t/saving-a-model-with-a-through-association/125740>\
**Category:** Rails\
**Created:** [January 11, 2008, 4:43pm UTC](https://www.ruby-forum.com/t/saving-a-model-with-a-through-association/125740 "2008-01-11T16:43:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [January 11, 2008, 4:43pm UTC](https://www.ruby-forum.com/t/saving-a-model-with-a-through-association/125740/1 "2008-01-11T16:43:10Z")

</div>

I can’t seem to find any logical way of handling this situation I have  
created.

Heres the problem I have worked myself into.

On the create/update of a model, say post, one of the things it has  
associated with it is a list of tags. The tags are associated through a  
separate model of tags\_to\_posts. But I wanted to separate the tags by  
the word, then save them.

On the PostController.update function i do a split of the tags based on  
the spaces. So the submitted params get broken into an array based on  
each word. But the save of this always seems to mess up.

This is basically how the situation is set up.

Models:

class Post \< ActiveRecord::Base  
has\_many :tags\_to\_post  
has\_many :tags, :through =\> :tags\_to\_post  
end

class Tag \< ActiveRecord::Base  
has\_many :tags\_to\_post  
has\_many :posts, :through =\> :tags\_to\_post  
end

class Tags\_to\_post \< ActiveRecord::Base  
belongs\_to :post  
belongs\_to :tag  
end

Edit.html.erb :

 Tags \<%= text\_field\_tag 'post[tags]', @post.tags.join(' '), :id =\> 'post\_tags' %\>

PostController  
def update  
@post = Post.find(params[:id])

```
params[:post][:tags] = params[:post][:tags].split

respond_to do |format|
  if @post.update_attributes(params[:post])
    flash[:notice] = 'Post was successfully updated.'
    format.html { redirect_to(@post) }
    format.xml { head :ok }
  else
    format.html { render :action => "edit" }
    format.xml { render :xml => @post.errors, :status =>

```

:unprocessable\_entity }  
end  
end  
end

Anyone have any suggestions for a good way of getting this to work?

---

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [January 11, 2008, 10:29pm UTC](https://www.ruby-forum.com/t/saving-a-model-with-a-through-association/125740/2 "2008-01-11T22:29:56Z")

</div>

Can anyone point me to a good example of how to setup a model with a  
has\_many :through association?

---

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [January 12, 2008, 8:00pm UTC](https://www.ruby-forum.com/t/saving-a-model-with-a-through-association/125740/3 "2008-01-12T20:00:46Z")

</div>

Any suggestions?

Last bump i guess then i’ll just let it die
