Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 保存模型时重复插入:通过关联_Ruby On Rails_Duplicates - Fatal编程技术网

Ruby on rails 保存模型时重复插入:通过关联

Ruby on rails 保存模型时重复插入:通过关联,ruby-on-rails,duplicates,Ruby On Rails,Duplicates,在下面的示例中,保存模型会导致重复插入 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: "01

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

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

你认为呢?

重复的插入是怎样的,它插入到3个不同的表中
shipping
orders
shippings
?请看下面显示的行两次:插入shippings(shipping_id,order_id,created_at,updated_at)值(3,6,'2016-03-14 15:04:15',2016-03-14 15:04:15')装运参数中有什么?装运参数={name:“Guy”,从国家:“US”,从城市:“a Ferry”,从州:“NY”,从邮政编码:“11232-1716”,从街道地址:“百老汇,1”,到国家:“US”,到城市:“idge”,到州:“MA”,到邮政编码:“01234-5678”,到街道地址:“马萨诸塞大道,1234”,服务代码:“地面”}奇怪的是,shippings数据是自动创建和复制的。我宁愿自己创建shipping数据,而不是shipping。保存以自动填充此数据。重复插入是如何进行的,它插入到3个不同的表中
shipping
orders
shippings
?请参见以下内容ng行显示两次:将值(3,6,'2016-03-14 15:04:15','2016-03-14 15:04:15','2016-03-14 15:04:15')插入发货(发货id,订单id,创建日期,更新日期)值(3,6,'2016-03-14 15:04:15')中
发货参数中的内容
?发货参数={姓名:“Guy”,来自国家:“美国”,来自城市:“一艘渡轮”,来自州:“纽约”,来自邮政编码:“11232-1716”,来自街道地址:“百老汇,1”,到国家:“美国”,到城市:“idge”,到州:“MA”,到邮政编码:“01234-5678”,到街道地址:“马萨诸塞大道,1234”,服务代码:“地面”}奇怪的是,船运数据自动创建并复制。我宁愿自己创建发货数据,而不是发货。保存以自动填充此数据。。
irb(main):014:0> @shipment.save
   (0.3ms)  BEGIN
  SQL (0.4ms)  INSERT INTO `shipments` (`name`, `from_country`, `from_city`, `from_state`, `from_postal_code`, `from_street_address`, `to_country`, `to_city`, `to_state`, `to_postal_code`, `to_street_address`, `service_code`, `created_at`, `updated_at`) VALUES ('121', 'US', 'ds Ferry', 'NY', '33-1716', 'd, 329', 'US', 'ss', 'ss', '1234-1228', 'r Avenue, 343', 'GROUND', '2016-03-14 15:04:15', '2016-03-14 15:04:15')
  SQL (5.0ms)  INSERT INTO `orders` (`created_at`, `updated_at`) VALUES ('2016-03-14 15:04:15', '2016-03-14 15:04:15')
  SQL (0.3ms)  INSERT INTO `shippings` (`shipment_id`, `order_id`, `created_at`, `updated_at`) VALUES (3, 6, '2016-03-14 15:04:15', '2016-03-14 15:04:15')
  SQL (0.3ms)  INSERT INTO `shippings` (`shipment_id`, `order_id`, `created_at`, `updated_at`) VALUES (3, 6, '2016-03-14 15:04:15', '2016-03-14 15:04:15')
   (4.1ms)  COMMIT
=> true