associated join data save problem

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) %>
  <div>
    <%=check_box_tag "content_master[tag_id][]", tag.id,
    @content_master.tags.include?(tag) %>
    <%= tag.name %>

  </div>
  <% 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..

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) %>


<%=check_box_tag “content_master[tag_ids][]”, tag.id,
@content_master.tags.include?(

tag) %>

            <%= [tag.name](http://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

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

Please Help..

changing from id to ids will get assigned value implicitly.

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

http://www.slideshare.net/ihower/rails-best-practices

It may useful to you.

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