使用has_many:通过关联保存模型时重复插入

时间:2016-03-14 15:27:10

标签: ruby-on-rails duplicates

在下面的示例中,保存模型会导致重复插入。

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:layout_margin="2dp"
        android:weightSum="7">

        <ToggleButton
            android:id="@+id/toggleSun"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:checked="false"
            android:maxLines="1"
            android:textOff="Sun"
            android:textOn="Sun" />

        <ToggleButton
            android:id="@+id/toggleMon"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:checked="false"
            android:maxLines="1"
            android:textOff="Mon"
            android:textOn="Mon" />

        <ToggleButton
            android:id="@+id/toggleTue"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:checked="false"
            android:maxLines="1"
            android:textOff="Tue"
            android:textOn="Tue" />

        <ToggleButton
            android:id="@+id/toggleWed"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:checked="false"
            android:maxLines="1"
            android:textOff="Wed"
            android:textOn="Wed" />

        <ToggleButton
            android:id="@+id/toggleThurs"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:checked="false"
            android:maxLines="1"
            android:textOff="Thu"
            android:textOn="Thu" />

        <ToggleButton
            android:id="@+id/toggleFri"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:checked="false"
            android:maxLines="1"
            android:textOff="Fri"
            android:textOn="Fri" />

        <ToggleButton
            android:id="@+id/toggleSat"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:checked="false"
            android:textOff="Sat"
            android:textOn="Sat" />

    </LinearLayout>

显示以下SQL:

shipment_params = {name: "Guy", from_country: "US", from_city: "a Ferry", from_state: "NY", from_postal_code: "11232-1716", from_street_address: "Broadway, 1", to_country: "US", to_city: "idge", to_state: "MA", to_postal_code: "01234-5678", to_street_address: "Massachusetts Avenue, 1234", service_code: "GROUND"} 
shipment = Shipment.new(shipment_params)
shipment.save!

class Shipment < ActiveRecord::Base
  has_many :shippings
  has_many :orders, :through => :shippings
end

class Shipping < ActiveRecord::Base
  belongs_to :order
  belongs_to :shipment
end

class Order < ActiveRecord::Base
  has_many :shippings
  has_many :transactions #, :class_name => "OrderTransaction"
  has_many :shipments, :through => :shippings
end

您怎么看?

0 个答案:

没有答案