# Associated join data save problem

**URL:** <https://www.ruby-forum.com/t/associated-join-data-save-problem/186849>\
**Category:** Rails\
**Created:** [April 13, 2010, 10:45am UTC](https://www.ruby-forum.com/t/associated-join-data-save-problem/186849 "2010-04-13T10:45:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rahul\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/43a26b/32.png) [@rahul\_m](https://www.ruby-forum.com/u/rahul_m)\
**Post date:** [April 13, 2010, 10:45am UTC](https://www.ruby-forum.com/t/associated-join-data-save-problem/186849/1 "2010-04-13T10:45:43Z")

</div>

Hiiii,

I have created one migration…

class TagContentMasterJoin \< ActiveRecord::Migration  
def self.up  
create\_table ‘content\_masters\_tags’ do |t|  
t.column ‘tag\_id’, :integer  
t.column ‘content\_master\_id’, :integer  
t.timestamps  
end  
end

def self.down  
drop\_table ‘content\_masters\_tags’  
end  
end  
for joining two tables  
and in content\_master view new want to save associated tag in the  
above table for that i have written this code in content\_master view  
new

```
<% for tag in Tag.find(:all) %>

```

 \<%=check\_box\_tag "content\_master[tag\_id][]", tag.id, @content\_master.tags.include?(tag) %\> \<%= tag.name %\> 
 \<% end %\>

and in my create function of content master controller following code  
is present …  
def create  
@content\_master = ContentMaster.new  
@content\_master.tags=params[:content\_master][:tag\_id]  
respond\_to do |format|  
if @content\_master.save  
#save images  
and some code here

but this all giving me this error  
ActiveRecord::AssociationTypeMismatch in Content  
mastersController#create

Tag(#46439460) expected, got String(#21132310)

RAILS\_ROOT: D:/TestingGeek/trunk/Site  
Application Trace | Framework Trace | Full Trace

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active\_record/  
associations/association\_proxy.rb:262:in `raise_on_type_mismatch' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/ associations/association_collection.rb:320:in`replace’  
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active\_record/  
associations/association\_collection.rb:320:in `each' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/ associations/association_collection.rb:320:in`replace’  
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active\_record/  
associations.rb:1325:in `tags=' D:/TestingGeek/trunk/Site/app/controllers/ content_masters_controller.rb:64:in`create’

and in my model code is  
has\_and\_belongs\_to\_many :content\_masters  
has\_and\_belongs\_to\_many :tags

i have give as respective model…

please help this is urgent…

thanks

rahul…

---

<div class="post-metadata">

**Author:** ![rahul\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/43a26b/32.png) [@rahul\_m](https://www.ruby-forum.com/u/rahul_m)\
**Post date:** [April 13, 2010, 11:23am UTC](https://www.ruby-forum.com/t/associated-join-data-save-problem/186849/2 "2010-04-13T11:23:11Z")

</div>

I assume that u r following models:

class ContentMaster \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :tags  
end

class Tags \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :content\_masters  
end

change ur view as:

```
   <% for tag in Tag.find(:all) %>
   <div>
           <%=check_box_tag "content_master[tag_id*s*][]", tag.id,
           @content_master.tags.include?(

```

> tag) %\>  
> \<%= tag.name %\>
> 
> ```
> </div>
> <% end %>
> 
> ```

Carefully look on the above code, I have changed tag\_id to tag\_ids.

Then you have to change the create method as follows:  
def create  
@content\_master = ContentMaster.new(params[:content\_master])

> ```
> respond_to do |format|
> if @content_master.save
> #save images
> 
> ```

The above code will perfectly work

---

<div class="post-metadata">

**Author:** ![rahul\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/43a26b/32.png) [@rahul\_m](https://www.ruby-forum.com/u/rahul_m)\
**Post date:** [April 13, 2010, 11:30am UTC](https://www.ruby-forum.com/t/associated-join-data-save-problem/186849/3 "2010-04-13T11:30:48Z")

</div>

Hiii K Arunmohan

thanks for reply…

i have change id to ids

but in controller create i can’t do the exact same  
because me having following code in my controller

def create  
@content\_master = ContentMaster.new  
@content\_master.title = params[:title]  
@content\_master.abstract\_text = params[:abstract\_text]  
#@content\_master.image\_path = imageObject.original\_path  
@content\_master.content\_text = params[:content\_text]  
@content\_master.promote= params[:promote]  
@content\_master.promote\_block= params[:promote\_block]  
@content\_master.draft\_state= params[:draft\_state]  
@content\_master.parent\_id=params[:content\_master][:parent\_id]  
@content\_master.tags=params[:content\_master][:tag\_ids]

if(params[:order\_no\_article]==’’)  
@content\_master.order\_no\_article = 0  
else  
@content\_master.order\_no\_article = params[:order\_no\_article]  
end

# @content\_master.promote = params[:promote]

```
#@metadata = @content_master.metadata.build(params[:metadata])
respond_to do |format|
  if @content_master.save

```

and it is still giving the same error…

please suggest the solution…

thanks

rahul

---

<div class="post-metadata">

**Author:** ![rahul\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/43a26b/32.png) [@rahul\_m](https://www.ruby-forum.com/u/rahul_m)\
**Post date:** [April 13, 2010, 12:44pm UTC](https://www.ruby-forum.com/t/associated-join-data-save-problem/186849/4 "2010-04-13T12:44:32Z")

</div>

changing from id to ids will get assigned value implicitly.

Check the 32 and 33rd slide of the following URL:

> **[Rails Best Practices](https://www.slideshare.net/ihower/rails-best-practices)**
>
> Rails Best Practices ihower@gmail.com 張文鈿 2009/10 As this slide writing, the current Rails version is 2.3.4 Who am I ? • 張文鈿 a.k.a. ihower • https://ihower.tw ...

It may useful to you.

---

<div class="post-metadata">

**Author:** ![rahul\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/43a26b/32.png) [@rahul\_m](https://www.ruby-forum.com/u/rahul_m)\
**Post date:** [April 13, 2010, 12:01pm UTC](https://www.ruby-forum.com/t/associated-join-data-save-problem/186849/5 "2010-04-13T12:01:49Z")

</div>

Please Help…

---

<div class="post-metadata">

**Author:** ![rahul\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/43a26b/32.png) [@rahul\_m](https://www.ruby-forum.com/u/rahul_m)\
**Post date:** [April 13, 2010, 1:29pm UTC](https://www.ruby-forum.com/t/associated-join-data-save-problem/186849/6 "2010-04-13T13:29:36Z")

</div>

Hiii K Arunmohan,

Now Add form is working by changing…  
@content\_master.tags=params[:content\_master][:tag\_ids]  
@content\_master.tag\_ids=params[:content\_master][:tag\_ids]

tags to tag\_ids

but in edit form there is one problem…  
when there is last entry which is selected and i want to unassign it ,  
it  
dont do…

why it is not doing…

in update method m having this simple code  
def update  
@content\_master = ContentMaster.find(params[:id])

```
respond_to do |format|
  if @content_master.update_attributes(params[:content_master])

```

i tried this below this  
@content\_master.tags.delete\_all(params[:content\_master]  
[:tag\_ids]).each{|i|@content\_master.tags \<\< Tag.find(i)}

but it is giving error of wrong number of argument…

please help…

thanks

rahul
