Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate action always creates two identical table entries instead of one
    text
    copied!<p>I have a create action in my ProductsController (the params come from a form in my view):</p> <pre><code>def create vendor = @current_vendor product = Product.create(:name =&gt; params[:product][:name]) vendor.products &lt;&lt; product vendor.belongings.create(:product_id =&gt; product.id, :count =&gt; params[:belonging][:count], :detail =&gt; params[:belonging][:detail]) if vendor.save flash[:notice] = "Produkt hinzugefügt!" redirect_back_or_default root_url else render :action =&gt; :new end end </code></pre> <ol> <li>It creates a variable "vendor", which stores the currently logged-in vendor (Authlogic)</li> <li>A new Product is created (the product name is from the input field in the form) and stored in the variable "product"</li> <li>The "product" is being connected to the current vendor</li> <li>In the belongings table, additional informations to the product are being stored</li> <li>it saves the whole thing</li> </ol> <p>It's a many-to-many relationship throught the belongings table.</p> <p><strong>My problem is, the create action always creates the product twice!</strong></p> <p>Thanks for your help! :)</p> <p>My console log when I create a new object through my form is:</p> <pre><code>Started POST "/products" for 127.0.0.1 at 2013-09-15 20:40:26 +0200 Processing by ProductsController#create as HTML Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"lNk/qQMP0xhlCuGgHtU+d5NEvIlCFcPSKB0FxDZH0zY=", "product"=&gt;{"name"=&gt;"Erdbeeren"}, "belonging"=&gt;{"count"=&gt;"20", "detail"=&gt;"Rot"}, "commit"=&gt;"Create"} DEPRECATION WARNING: ActiveRecord::Base#with_scope and #with_exclusive_scope are deprecated. Please use ActiveRecord::Relation#scoping instead. (You can use #merge to merge multiple scopes together.). (called from current_vendor_session at /Users/reto_gian/Desktop/dici/app/controllers/application_controller.rb:11) Vendor Load (0.3ms) SELECT "vendors".* FROM "vendors" WHERE "vendors"."persistence_token" = '04f75db0e2ef108ddb0ae1be1da167536d47b4d79c60ecb443ad2ea5717ecd752388e581f9379746568c72372be4f08585aa5581915b1be64dc412cded73a705' LIMIT 1 (0.1ms) begin transaction SQL (0.8ms) INSERT INTO "products" ("created_at", "name", "updated_at") VALUES (?, ?, ?) [["created_at", Sun, 15 Sep 2013 18:40:26 UTC +00:00], ["name", "Erdbeeren"], ["updated_at", Sun, 15 Sep 2013 18:40:26 UTC +00:00]] (0.8ms) commit transaction (0.1ms) begin transaction SQL (0.6ms) INSERT INTO "belongings" ("created_at", "product_id", "updated_at", "vendor_id") VALUES (?, ?, ?, ?) [["created_at", Sun, 15 Sep 2013 18:40:26 UTC +00:00], ["product_id", 7], ["updated_at", Sun, 15 Sep 2013 18:40:26 UTC +00:00], ["vendor_id", 1]] (0.9ms) commit transaction (0.1ms) begin transaction SQL (0.6ms) INSERT INTO "belongings" ("count", "created_at", "detail", "product_id", "updated_at", "vendor_id") VALUES (?, ?, ?, ?, ?, ?) [["count", "20"], ["created_at", Sun, 15 Sep 2013 18:40:26 UTC +00:00], ["detail", "Rot"], ["product_id", 7], ["updated_at", Sun, 15 Sep 2013 18:40:26 UTC +00:00], ["vendor_id", 1]] (0.9ms) commit transaction Redirected to http://localhost:3000/ Completed 302 Found in 30ms (ActiveRecord: 5.1ms) </code></pre>
 

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