Note that there are some explanatory texts on larger screens.

plurals
  1. PONested Attributes not saving to Activerecord
    primarykey
    data
    text
    <p>I've been Stack Overflowing for a while now, and I've done some tutorials/walkthroughs across the web including RailsGuides &amp; Railscasts, but I seem to be having trouble saving my nested attributes to my database. </p> <p>I have Wagers, which have many Terms</p> <pre><code>class Wager &lt; ActiveRecord::Base has_and_belongs_to_many :users belongs_to :host, class_name: "User", foreign_key: "host_id" belongs_to :guest, class_name: "User", foreign_key: "guest_id" has_many :terms, :dependent =&gt; :destroy accepts_nested_attributes_for :terms, allow_destroy: true , :reject_if =&gt; lambda { |a| a[:terms].blank? } attr_accessible :title, :description, :terms, :terms_attributes end </code></pre> <p>And Terms</p> <pre><code>class Term &lt; ActiveRecord::Base belongs_to :wager attr_accessible :title, :body, :criterion, :host_criterion, :guest_criterion, :terms_attributes end </code></pre> <p>My Wagers controller</p> <pre><code>class WagersController &lt; ApplicationController before_filter :authenticate_user! def index end def create # Create new wager from params @wager = Wager.new(params[:wager]) # Assign current user @wager.host_id = current_user.id # Assign guest user here # ______________________ @term = @wager.terms.build(params[:terms]) if @wager.valid? respond_to do |format| if @wager.save format.html { redirect_to(@wager, :notice =&gt; 'Wager Stub successfully created.') } format.xml { render :xml =&gt; @wager, :status =&gt; :created, :location =&gt; @wager } else format.html { render :action =&gt; "new" } format.xml { render :xml =&gt; @wager.errors, :status =&gt; :unprocessable_entity } end end end def new @wager = Wager.new @term = Term.new 3.times { @wager.terms.build } end end </code></pre> <p>My new view:</p> <pre><code>&lt;h2&gt;Create a Wager&lt;/h2&gt; &lt;%= form_for @wager, url: {action: "create"}, html: {class: ""} do |f| %&gt; &lt;fieldset&gt; &lt;legend&gt;Create a wager&lt;/legend&gt; &lt;div class="row"&gt; &lt;div class="large-12 columns"&gt; &lt;label&gt;Title&lt;/label&gt; &lt;%= f.text_field :title, placeholder: "The Bet To End All Bets" %&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="row"&gt; &lt;div class="large-4 columns"&gt; &lt;label&gt;With&lt;/label&gt; &lt;%#= f.text_field :guest, placeholder: "Choose a Friend" %&gt; &lt;/div&gt; &lt;div class="large-8 columns"&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="row"&gt; &lt;div class="large-12 columns"&gt; &lt;label&gt;Description&lt;/label&gt; &lt;%= f.text_area :description, size: "60x12", placeholder: "What's all this about?" %&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="row"&gt; &lt;div class="large-5 columns"&gt; &lt;div class="row collapse"&gt; &lt;h4&gt;Your Terms&lt;/h4&gt; &lt;/div&gt; &lt;%= f.fields_for :terms do |term_form| %&gt; &lt;%= term_form.label :host_criterion, 'Term:' %&gt; &lt;%= term_form.text_field :host_criterion %&gt; &lt;% end %&gt; &lt;/div&gt; &lt;div class="large-5 columns"&gt; &lt;div class="row collapse"&gt; &lt;h4&gt;Their Terms&lt;/h4&gt; &lt;/div&gt; &lt;%= f.fields_for :terms do |term_form| %&gt; &lt;%= term_form.label :guest_criterion, 'Term:' %&gt; &lt;%= term_form.text_field :guest_criterion %&gt; &lt;% end %&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="row"&gt; &lt;div class="large-8 columns"&gt; &lt;/div&gt; &lt;div class="large-4 columns"&gt; &lt;%= f.submit "Create Stub", class: "button" %&gt; &lt;/div&gt; &lt;/div&gt; &lt;/fieldset&gt; </code></pre> <p>My params look like this: (SPOILER ALERT: They're all nil. Except it does assign a wager ID. Pretty sure that's due to the build method)</p> <pre><code>=&gt; "wager"=&gt; {"title"=&gt;"Title of the Bet", "description"=&gt;"Bet description", "terms_attributes"=&gt; {"0"=&gt;{"host_criterion"=&gt;"You gotta be nude"}, "1"=&gt;{"host_criterion"=&gt;"no drinking"}, "2"=&gt;{"host_criterion"=&gt;"can't brush your hair"}, "3"=&gt;{"guest_criterion"=&gt;"Guest Term 1"}, "4"=&gt;{"guest_criterion"=&gt;"Guest Term 2"}, "5"=&gt;{"guest_criterion"=&gt;"Guest Term 3"}}}, "commit"=&gt;"Create Stub", "action"=&gt;"create", "controller"=&gt;"wagers"} </code></pre> <p>Yet when the record is saved, my server log says:</p> <pre><code>User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 5 LIMIT 1 (0.6ms) begin transaction SQL (1.9ms) INSERT INTO "wagers" ("created_at", "deadline", "description", "guest_id", "host_id", "title", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?) [["created_at", Thu, 17 Oct 2013 23:50:50 UTC +00:00], ["deadline", nil], ["description", "Bet description"], ["guest_id", nil], ["host_id", 5], ["title", "Title of the Bet"], ["updated_at", Thu, 17 Oct 2013 23:50:50 UTC +00:00]] SQL (0.3ms) INSERT INTO "terms" ("created_at", "criterion", "guest_criterion", "host_criterion", "updated_at", "wager_id") VALUES (?, ?, ?, ?, ?, ?) [["created_at", Thu, 17 Oct 2013 23:50:50 UTC +00:00], ["criterion", nil], ["guest_criterion", nil], ["host_criterion", nil], ["updated_at", Thu, 17 Oct 2013 23:50:50 UTC +00:00], ["wager_id", 48]] (1.0ms) commit transaction Redirected to http://localhost:3000/wagers/48 Completed 302 Found in 121668ms (ActiveRecord: 5.9ms) </code></pre> <p>What am I missing? The goal is to create a term for each of the terms (up to 6) submitted along with the wager. I'd appreciate any help on this as the walkthroughs I've found online have all for the most part led me to this point, but it just ends up working for them (Rails sugar style), validations and all. :-/</p> <p>Running Rails 3.2.13.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload