problema has_many + accepts_nested_attributes_for

Hi, I have one relationship ONE to N and need to populate the N in the
same form that have "Ad" model.
Below is my code from this problem...
I need to create a few checkbox populated using model "Options" and
what was selected by user need to save into model "OptionAnswer" that
is related with "Ad" model.
If I was too much confusing please say it and I'll try to explain in
another way.

Thanks

--- models
class Ad < ActiveRecord::Base
  has_many :optionanswers, :class_name => "OptionAnswer", :foreign_key
=> "ad_id", :dependent => :destroy

  accepts_nested_attributes_for :optionanswers
end

class OptionAnswer < ActiveRecord::Base
  belongs_to :ad
  belongs_to :option
end

class Option < ActiveRecord::Base
  has_many :optionanswer
end

--- controller
  def new
    @ad = Ad.new
    @ad.optionanswers.build
  end

--- view
<%= form_for(@ad, :url => { :action => "create" }) do |f| %>

  <% f.fields_for :optionanswers do |build_answer| %>

  <% for option_a in Option.find(:all, :conditions => {:option_type_id
=> 2}) %>

    <div>
    <%= build_answer.check_box :option_id option_a.id %>&nbsp;<%=
option_a.text %>
    </div>

    <% end %>

  <% end %>

<% end %>

Hi, I have one relationship ONE to N and need to populate the N in the
same form that have "Ad" model.
Below is my code from this problem...
I need to create a few checkbox populated using model "Options" and
what was selected by user need to save into model "OptionAnswer" that
is related with "Ad" model.
If I was too much confusing please say it and I'll try to explain in
another way.

So Ad has_many options, :through => :optionanswers?
If you want the user to pick only from existing options, then you
don't actually need nested_attributes - create checkboxes with a name
of ad[option_ids][] and whose values are the ids of the options.

This works because with a has many through, you get an option_ids=
method

Fred

--- MODELS
class Ad < ActiveRecord::Base
  belongs_to :user
  has_many :options, :through => :optionanswers
end

class OptionAnswer < ActiveRecord::Base
  belongs_to :ad
  belongs_to :option
end

class Option < ActiveRecord::Base
  belongs_to :optiontype
  has_many :ads, :through => :optionanswers
end

--- CONTROLLER
  def create
    @ad = Ad.new(params[:ad])

    if @ad.save
      flash[:success] = "Ads saved with success!"
      redirect_to @ad
    else
      render 'new'
    end
  end

--- VIEW (parts important)
  <% for option_a in Option.find(:all, :conditions => {:option_type_id
=> 2}) %>
  <div>
    <%= check_box_tag "ad[option_ids][]", option_a.id %>&nbsp;<%=
option_a.text %>
  </div>
<% end %>

---- ERROR
ActiveRecord::HasManyThroughAssociationNotFoundError in
EscortController#create

Could not find the association :optionanswers in model Ad
Rails.root: /Users/celestinoruas/Documents/Dropbox/railsprojects/
clasisex3

Application Trace | Framework Trace | Full Trace
app/controllers/escort_controller.rb:27:in `new'
app/controllers/escort_controller.rb:27:in `create'

If you can help me I'll be great full.

Regards

Like the error message says, you still need the optionanswers
association.

Fred

Hi, worked, thanks a lot.

Regards