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

--
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@googlegroups.com.
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.

Search Discussions

  • Frederick Cheung at Dec 11, 2011 at 11:35 am

    On Dec 11, 1:19 am, Sp4wnX 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 %>&nbsp;<%=
    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@googlegroups.com.
    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.
  • Sp4wnX at Dec 11, 2011 at 11:39 pm
    --- 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


    On Dec 11, 12:35 pm, Frederick Cheung wrote:
    On Dec 11, 1:19 am, Sp4wnX 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 %>&nbsp;<%=
    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@googlegroups.com.
    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 at Dec 12, 2011 at 7:31 am

    On Dec 11, 11:38 pm, Sp4wnX 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@googlegroups.com.
    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 at Dec 12, 2011 at 8:58 pm
    Hi, worked, thanks a lot.

    Regards
    On Dec 12, 2011, at 8:31 AM, Frederick Cheung wrote:


    On Dec 11, 11:38 pm, Sp4wnX 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@googlegroups.com.
    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.
    --
    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@googlegroups.com.
    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedDec 11, '11 at 10:59a
activeDec 12, '11 at 8:58p
posts5
users2
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2021 Grokbase