Help with many-to-many nested form

I have a form used to create a ‘Style’. The style may have any number of ‘features’ which correspond to ‘Feature’ models in the features table. I would like to add the appropriate entries to a relationship table (‘stylefeatures’) whenever a Style is created. Here is a breakdown of my models:

class Style < ActiveRecord::Base

  has_many :stylefeatures

  has_many :features, :through => :stylefeatures, :foreign_key => :feature_id

  accepts_nested_attributes_for :stylefeatures

end

class Feature < ActiveRecord::Base

  has_many :stylefeatures

  has_many :styles, :through => :stylefeatures, :foreign_key => :style_id

end

class Stylefeature < ActiveRecord::Base

  belongs_to :style

  belongs_to :feature

  accepts_nested_attributes_for :features

end

…and here is my controller for the ‘new’ action:

  def new

    @style = Style.new

    @features = Feature.all

  end

…and here is my form:

<%= simple_form_for @style, :html => { :class => 'form-horizontal' } do |m| %>

  <%= m.simple_fields_for :features do |p| %>

    <%= p.input :name, :label => "Features", :collection => @features, :input_html => { :multiple => true } %>

  <% end %>

  [....]
<% end %>

Right, so the list of features DOES load up into the multiple select box within my form. So that’s sweet. However, when I go to submit I receive the error: No association found for name 'features'. Has it been defined yet?

I get the exact same error when adding @style.features.build to the controller ‘new’ action.

This is what the params hash looks like. It carries the ID of the feature name selected.

"features"=>{"name"=>["","7"]}

So my question is, how do I go about loading the features listed in the features table for the nested form, and on form submission, handle the selected features for addition to the stylefeatures relationship table.

Thanks very much

I have a form used to create a 'Style'. The style may have any number of 'features' which correspond to 'Feature' models in the features table. I would like to add the appropriate entries to a relationship table ('stylefeatures') whenever a Style is created. Here is a breakdown of my models:

class Style < ActiveRecord::Base

has_many :stylefeatures

has_many :features, :through => :stylefeatures, :foreign_key => :feature_id

accepts_nested_attributes_for :stylefeatures

end

class Feature < ActiveRecord::Base

has_many :stylefeatures

has_many :styles, :through => :stylefeatures, :foreign_key => :style_id

end

class Stylefeature < ActiveRecord::Base

belongs_to :style

belongs_to :feature

accepts_nested_attributes_for :features

I don't know whether this is part or all of the problem, but you can't have :features here as it is belongs_to, so it should be singular (if you need it at all).

By the way please don't post html, or at least not complex html, it makes the reply quoting very messy as you can see.

Colin

Colin, thanks for your response. I have moved a bit further on my original problem of updating the through table.

I now have in my controller:

def new

@style = Style.new

@style.stylefeatures.build

@features = Feature.all

end

def create

@style = Style.new(params[:style])

@style.stylefeatures.build

@style.save

end

…and in my style model

attr_accessible :stylefeatures_attributes

has_many :stylefeatures

has_many :features, :through => :stylefeatures, :foreign_key => :feature_id

accepts_nested_attributes_for :stylefeatures

… and in my stylefeature model

belongs_to :style

belongs_to :feature

accepts_nested_attributes_for :feature

… and in my feature model

attr_accessible :description, :fullname, :name

has_many :stylefeatures

has_many :styles, :through => :stylefeatures, :foreign_key => :style_id

… and in my create form

<%= m.simple_fields_for :stylefeatures do |p| %>

<%= p.input :feature_id, :label => “Features”, :collection => @features, :input_html => { :multiple => true } %>

<% end %>

Now when I save the new Style, the stylefeatures table is updated with the appropriate style_id, but with two useless entries. The first is an array with all of the feature ids which have been selected in the form. The second is a blank entry with the appropriate style_id and nothing in the feature_id column.

Do you have any clue of what I might be doing wrong, or how to spread the collected feature_id’s into the table as desired?

Thanks, Abram

https://github.com/vishalsingh/multiple_upload_photo check this may be helpful for you