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