Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to save attributes to a has_many :through join table with no existing records to build from
    primarykey
    data
    text
    <p>I have a form which creates a new Child record and a new Parent record using <code>accepts_nested_attributes_for</code></p> <p>Children and Parents have a <code>has_many :through</code> association like so:</p> <pre><code>class Child &lt; ActiveRecord::Base has_many :relationships has_many :parents, through: :relationships accepts_nested_attributes_for :parents accepts_nested_attributes_for :relationships, allow_destroy: true end class Parent &lt; ActiveRecord::Base has_many :relationships has_many :children, through: :relationships end class Relationship &lt; ActiveRecord::Base belongs_to :child belongs_to :parent accepts_nested_attributes_for :parents #has :schedule attribute end </code></pre> <p>I would like to set the <code>schedule</code> attribute in Relationships at the same time and can't use <code>accepts_nested_attributes_for</code> as I am manipulating some data in the controller to determine the <code>schedule</code> object before saving.</p> <p>There are many answers on stack overflow for accomplishing this with a pre-existing parent record but I am having trouble finding any for a situation where none of the associations exist previously.</p> <p>I have tried to do this with an <code>after_create</code> callback in the model but don't have access to <code>params</code> and have read that it is bad procedure to do so anyway. ( <a href="https://stackoverflow.com/questions/4725185/rails-how-to-pass-params-from-controller-to-after-save-inside-model">Rails How to pass params from controller to after_save inside model</a>).</p> <p>Ideally I would like to set <code>schedule</code> in the same database call that creates the relationship record along with <code>child_id</code> and <code>parent_id</code>.</p> <p>Any help with this would be awesome, thanks</p> <p><strong>UPDATE</strong></p> <p>Controller as of original post</p> <pre><code>def new @child = Child.new end def create @child = Child.new(params[:child]) schedule = build_schedule(params) # Need to save schedule to @child.relationship[0].schedule somehow if @child.save! redirect_to admins_path else render 'new' end end private def build_schedule(params) # works with params[:mon, :tue, :wed, :thu, :fri] to return a schedule object. end </code></pre> <p>The <code>parent</code> is built using <a href="http://railscasts.com/episodes/196-nested-model-form-revised?view=comments" rel="nofollow noreferrer">Ryan Bates' nested form helper</a>. This is the build line from it:</p> <pre><code>module ApplicationHelper def link_to_add_fields(name, f, association) new_object = f.object.send(association).klass.new ... end end </code></pre> <p>I am not building the <code>relationship</code> at all, it is magically built by rails as far as i'm aware.</p> <p>Trying <strong>Danny Van Hoof's</strong> suggestion <code>@child.relationships[0].schedule =</code> i get <code>undefined method schedule=' for nil:NilClass</code> as no relationship exists.</p> <p>If i build the relationship beforehand either in <code>new</code> or <code>create</code> action using <code>@child.relationship.build</code> I get 2 relationship records being saved:</p> <p><img src="https://i.imgur.com/446q51J.png" alt="relationships table"></p> <p>The one that I built has <code>schedule</code> saved but has no <code>parent_id</code> (parental_group_id) The second record is the one build magically and has both correct ids but no <code>schedule</code>.</p> <p>Now when trying <strong>Rick Peck's</strong> suggestion by adding</p> <pre><code>def params params.require(:child).permit(relationships_attributes: [:schedule]) # tried with and without `parents_attributes` end </code></pre> <p>I get a <code>stack level too deep</code> error on the <code>params.require</code> line. I also don't fully understand what this does. (I'm fairly new to rails)</p> <p>Thanks for helping!</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.
 

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