Note that there are some explanatory texts on larger screens.

plurals
  1. POnested attribute object is created three times instead of one
    text
    copied!<p>I have three models, booking, room and travellers.</p> <p>Booking has many rooms room has many travellers</p> <p>Since I'm doing a multi-step wizard booking and rooms gets created first, the travellers are created later in the update action.</p> <p>This is the log for the update action: <a href="http://pastie.org/private/it7onlg8bnurqkgv6mptrq" rel="nofollow">http://pastie.org/private/it7onlg8bnurqkgv6mptrq</a></p> <p>And this is the relevant methods and actions for creating the travellers: </p> <h2>The view action</h2> <pre><code>def step3 @booking = Booking.find_by_random_url_key(params[:id]) @variant = @booking.variant @booking.rooms.collect {|room| room.number_of_persons.times {room.travellers.build} if room.travellers.blank?} render :partial =&gt; "bookings/partials/step3", :layout =&gt; "booking" end </code></pre> <p><strong>room.number_of_persons method just return an int.</strong></p> <h2>Relevant part of the update action</h2> <pre><code>.. elsif @booking.update_attributes(params[:booking]) &amp;&amp; @booking.aasm_state == "step3" redirect_to booking_step4_url(@booking) @booking.next! .. </code></pre> <p><strong>next! is just a aasm transition</strong></p> <p>If I do a create in the console</p> <pre><code>Room.last.travellers.create(:forename =&gt; "john", :country_name =&gt; "Germany") </code></pre> <p>Only one object is created and even if I go back in the view and submit again he correctly updates the created object and does not create new ones.</p>
 

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