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 %> <%option_a.text %> </div> <% end %> <% end %> <% end %> -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Dec-11 11:35 UTC
Re: problema has_many + accepts_nested_attributes_for
On Dec 11, 1:19 am, Sp4wnX <celestinor...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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_idsmethod Fred> > 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 %> <%> option_a.text %> > </div> > > <% end %> > > <% end %> > > <% end %>-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
--- 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 %> <%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 On Dec 11, 12:35 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Dec 11, 1:19 am, Sp4wnX <celestinor...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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 > > > > > > > > > > > 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 %> <%> > option_a.text %> > > </div> > > > <% end %> > > > <% end %> > > > <% end %>-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Dec-12 07:31 UTC
Re: problema has_many + accepts_nested_attributes_for
On Dec 11, 11:38 pm, Sp4wnX <celestinor...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > ---- ERROR > ActiveRecord::HasManyThroughAssociationNotFoundError in > EscortController#create > > Could not find the association :optionanswers in model Ad > Rails.root: /Users/celestinoruas/Documents/Dropbox/railsprojects/ > clasisex3Like the error message says, you still need the optionanswers association. Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Celestino Ruas
2011-Dec-12 20:58 UTC
Re: problema has_many + accepts_nested_attributes_for
Hi, worked, thanks a lot. Regards On Dec 12, 2011, at 8:31 AM, Frederick Cheung wrote:> > > On Dec 11, 11:38 pm, Sp4wnX <celestinor...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> >> ---- ERROR >> ActiveRecord::HasManyThroughAssociationNotFoundError in >> EscortController#create >> >> Could not find the association :optionanswers in model Ad >> Rails.root: /Users/celestinoruas/Documents/Dropbox/railsprojects/ >> clasisex3 > > Like the error message says, you still need the optionanswers > association. > > Fred > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.