Note that there are some explanatory texts on larger screens.

plurals
  1. POassociation id not getting set using accepts_nested_attributes_for and decent_exposure
    primarykey
    data
    text
    <p>When I post a form to create a new inquiry with a child comment (in the app, inquiries can have multiple comments), the comment is not getting built. It works when remove the presence validations. So it has to do with the order in which things are built and saved. How to preserve the validations and keep the code clean?</p> <p>(The following is an example so it may not be exactly runable)</p> <p>models/inquiry.rb</p> <pre><code>class Inquiry &lt; ActiveRecord::Base has_many :comments accepts_nested_attributes_for :comments </code></pre> <p>models/comment.rb</p> <pre><code>class Comment &lt; ActiveRecord::Base belongs_to :inquiry belongs_to :user validates_presence_of :user_id, :inquiry_id </code></pre> <p>controllers/inquiry_controller.rb</p> <pre><code>expose(:inquiries) expose(:inquiry) def new inquiry.comments.build :user =&gt; current_user end def create # inquiry.save =&gt; false # inquiry.valid? =&gt; false # inquiry.errors =&gt; {:"comments.inquiry_id"=&gt;["can't be blank"]} end </code></pre> <p>views/inquiries/new.html.haml</p> <pre><code>= simple_form_for inquiry do |f| = f.simple_fields_for :comments do |c| = c.hidden_field :user_id = c.input :body, :label =&gt; 'Comment' = f.button :submit </code></pre> <p>database schema</p> <pre><code>create_table "inquiries", :force =&gt; true do |t| t.string "state" t.datetime "created_at" t.datetime "updated_at" end create_table "comments", :force =&gt; true do |t| t.integer "inquiry_id" t.integer "user_id" t.text "body" t.datetime "created_at" t.datetime "updated_at" end </code></pre>
    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.
 

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