habtm in nested form

Hi! I have next

====== models
require 'paperclip'
class User < ActiveRecord::Base
  has_one :user_settings
end

class UserSettings < ActiveRecord::Base
has_one :habits_and_past
accepts_nested_attributes_for :habits_and_past
end

class HabitsAndPast < ActiveRecord::Base
        has_and_belongs_to_many :musics
        accepts_nested_attributes_for :musics
end

class Music < ActiveRecord::Base
  has_and_belongs_to_many :habits_and_pasts

  def display_name
    "#{name}"
  end
end

====== migrations
for all models

class HabitsAndPastsMusics < ActiveRecord::Migration
  def self.up
      create_table :habits_and_pasts_musics, :id => false do |t|
        t.references :habits_and_past
        t.references :music
      end
  end

  def self.down
      drop_table :habits_and_pasts_musics
  end
end

====== in users_controller
  def saveprofile
     @user = User.find(params[:id])
     @user_settings = UserSettings.new(params[:user_settings])
      if @user_settings.save
        flash[:notice] = 'message'
        render :action => "show"
      else
        render :action => "addprofile"
      end
  end

====== in add_profile.html.erb
<% form_for :user_settings, @user_settings,
:url=>{:action=>"saveprofile"} do |f|%>
<%f.hidden_field :user_id, :value => @user.id%>
<%= render :partial => "user_settings", :locals => { :f => f, :button =>
"Submit"} %>
<% end %>

====== _user_settings.html.erb
......
<% f.fields_for :habits_and_past_attributes do |habit| %>
......
<p>
  <%= label("user_settings[habits_and_past_attributes]", "musics_list" ,
"Musics") %><br />
   <% Music.find(:all).each do |music| %>
    <%=
check_box_tag("user_settings[habits_and_past_attributes][musics_list]
[#{music.id}]","1",false) %><%= "#{music.display_name}"%><br />
  <% end %>
</p>
......
<%end%>

======== when I press submit I see error

ActiveRecord::UnknownAttributeError in UsersController#saveprofile

unknown attribute: musics_list

======= in params

"habits_and_past_attributes"=>{"favourite_book"=>"",
"favourite_group"=>"",
"favourite_film"=>"",
"musics_list"=>{"1"=>"1",
"2"=>"1"},

I'm using rails 2.3.5

Question - How to save list of musics that was selected ?

Bohdan Pohoriletz wrote:

Hi! I have next

====== models
require 'paperclip'
class User < ActiveRecord::Base
  has_one :user_settings
end

class UserSettings < ActiveRecord::Base
has_one :habits_and_past
accepts_nested_attributes_for :habits_and_past
end

class HabitsAndPast < ActiveRecord::Base
        has_and_belongs_to_many :musics
        accepts_nested_attributes_for :musics
end

class Music < ActiveRecord::Base
  has_and_belongs_to_many :habits_and_pasts

  def display_name
    "#{name}"
  end
end

====== migrations
for all models

class HabitsAndPastsMusics < ActiveRecord::Migration
  def self.up
      create_table :habits_and_pasts_musics, :id => false do |t|
        t.references :habits_and_past
        t.references :music
      end
  end

  def self.down
      drop_table :habits_and_pasts_musics
  end
end

====== in users_controller
  def saveprofile
     @user = User.find(params[:id])
     @user_settings = UserSettings.new(params[:user_settings])
      if @user_settings.save
        flash[:notice] = 'message'
        render :action => "show"
      else
        render :action => "addprofile"
      end
  end

====== in add_profile.html.erb
<% form_for :user_settings, @user_settings,
:url=>{:action=>"saveprofile"} do |f|%>
<%f.hidden_field :user_id, :value => @user.id%>
<%= render :partial => "user_settings", :locals => { :f => f, :button =>
"Submit"} %>
<% end %>

====== _user_settings.html.erb
......
<% f.fields_for :habits_and_past_attributes do |habit| %>
......
<p>
  <%= label("user_settings[habits_and_past_attributes]", "musics_list" ,
"Musics") %><br />
   <% Music.find(:all).each do |music| %>
    <%=
check_box_tag("user_settings[habits_and_past_attributes][musics_list]
[#{music.id}]","1",false) %><%= "#{music.display_name}"%><br />
  <% end %>
</p>
......
<%end%>

======== when I press submit I see error

ActiveRecord::UnknownAttributeError in UsersController#saveprofile

unknown attribute: musics_list

======= in params

"habits_and_past_attributes"=>{"favourite_book"=>"",
"favourite_group"=>"",
"favourite_film"=>"",
"musics_list"=>{"1"=>"1",
"2"=>"1"},

I'm using rails 2.3.5

Question - How to save list of musics that was selected ?

Solved

I added :join_table option=>"" in both models and changed
====== _user_settings.html.erb
......
<% f.fields_for :habits_and_past_attributes do |habit| %>
......
<p>
   <%= label("user_settings[habits_and_past_attributes]", "music_ids" ,
"Musics") %><br />
    <% Music.find(:all).each do |music| %>
     <%=
check_box_tag("user_settings[habits_and_past_attributes][music_ids]
[]","#{music.id}",false) %><%= "#{music.display_name}"%><br />
   <% end %>
</p>
......
<%end%>