在has_many中创建的重复记录:通过关联

时间:2011-02-21 07:00:48

标签: ruby-on-rails associations has-many-through duplicate-data

我有三种型号:Booking,ExtraNight和BookedExtraNight。运行Rails 2.3.11

预订

has_many :extra_nights, :through => :booked_extra_nights
has_many :booked_extra_nights, :dependent => :destroy

ExtraNight:

has_many :booked_extra_nights, :dependent => :destroy
has_many :bookings, :through => :booked_extra_nights

BookedExtraNight:

belongs_to  :extra_night
belongs_to  :booking

现在,由于应用程序的工作方式,在创建了registered_extra_night时,预订就存在了。使用“预订更新”操作创建registered_extra_night。 在视图中,使用以下内容构建了expected_extra_night:

<% unless @booking.booked_extra_nights.exists? %>
   <% @booking.booked_extra_nights.build %>
<% end %>

我使用nested_form_for @booking和f.fields_for:booked_extra_nights来创建registered_extra_nights。

现在一切正常,直到我创建了expected_extra_night的两个副本时按下提交(更新操作)。

见日志:

Processing BookingsController#update (for 127.0.0.1 at 2011-02-21 07:44:22) [PUT]
Parameters: {"action"=>"update", "_method"=>"put",   "authenticity_token"=>"b/M+VjMxA8RFqbubhBeF494B/zhxi/2Eb3EtoCoRLx0=",  "id"=>"5b2jwg7qw5na3vz4nt", "booking"=>{"booked_extra_nights_attributes"=>{"0"=> {"number_of_days"=>"2", "from_date(1i)"=>"2011", "from_date(2i)"=>"9", "from_date(3i)"=>"1",  "_destroy"=>"", "extra_night_id"=>"7"}}}, "controller"=>"bookings"}
Booking Load (1.3ms)   SELECT * FROM "bookings" WHERE ("bookings"."random_url_key" =  '5b2jwg7qw5na3vz4nt') LIMIT 1
Variant Load (0.6ms)   SELECT * FROM "variants" WHERE ("variants"."id" = 27) 
SQL (0.1ms)   BEGIN
SQL (0.7ms)   INSERT INTO "booked_extra_nights" ("number_of_days", "created_at",   "updated_at", "booking_id", "from_date", "extra_night_id") VALUES(2, '2011-02-21  06:44:22.525154', '2011-02-21 06:44:22.525154', 69, '2011-09-01', 7) RETURNING "id"
SQL (0.8ms)   COMMIT
SQL (0.6ms)   BEGIN
SQL (0.6ms)   INSERT INTO "booked_extra_nights" ("number_of_days", "created_at",  "updated_at", "booking_id", "from_date", "extra_night_id") VALUES(2, '2011-02-21 06:44:22.544452', '2011-02-21 06:44:22.544452', 69, '2011-09-01', 7) RETURNING "id"
SQL (25.8ms)   COMMIT
SQL (0.1ms)   BEGIN
Booking Update (0.6ms)   UPDATE "bookings" SET "updated_at" = '2011-02-21 06:44:22.575409', "aasm_state" = 'step3' WHERE "id" = 69
SQL (0.5ms)   COMMIT
Redirected to http://localhost:3000/bookings/5b2jwg7qw5na3vz4nt/step3

正如你可以看到创建了两个相同的记录,现在如果我要构建4个registered_extra_nights并按提交,我最终会得到8条记录。

我还发现,如果我在创建预订的同时创建了expected_extra_night记录,那么我可以添加尽可能多的内容,而不需要重复。 据我所知,这发生在所有2.3.x版本的rails上,所以这显然是我做错了。任何帮助都会非常感激,因为它正在努力。

谢谢!

2 个答案:

答案 0 :(得分:0)

我也有这个问题,我的解决方案在我的控制器中,不使用构建,并使用new并提供parent参数。

在我的代码中提取.. 而不是使用

@new_brand = @company.brands.build/new

我使用

@new_brand = Brand.new(:company => @company)

第一个将此对象作为空对象添加到@company,然后当您在创建操作中提交另一个新对象时。

第二个只是为了表单的目的在内存中创建一个新对象,但是当值被提交到create动作时,没有与公司绑定的对象

答案 1 :(得分:0)

谢谢,Rabbot!你让我在正确的轨道,伤心地说,但update操作是一个烂摊子。 我已经重新考虑了它,现在一切正常,我认为双重保存与我首先更新预订,然后重定向,然后通过AASM再次更新预订。

以下是旧的更新操作(我告诉你这是一团糟):

def update
  @booking = Booking.find_by_random_url_key(params[:id])
  @variant = @booking.variant
  if params[:back_button]
    if @booking.aasm_state == "step2"
      redirect_to booking_step1_url(@booking)
    elsif @booking.aasm_state == "step3"
      redirect_to booking_step2_url(@booking)
    elsif @booking.aasm_state == "step4"
      redirect_to booking_step3_url(@booking)
    elsif @booking.aasm_state == "step5"
      redirect_to booking_step4_url(@booking)
    end
    @booking.previous!
  else
    if @booking.update_attributes(params[:booking]) && @booking.aasm_state == "step1"
      redirect_to booking_step2_url(@booking)
      @booking.next!
    elsif @booking.update_attributes(params[:booking]) && @booking.aasm_state == "step2"
      @booking.next!
      redirect_to booking_step3_url(@booking)
    elsif @booking.update_attributes(params[:booking]) && @booking.aasm_state == "step3"
      redirect_to booking_step4_url(@booking)
      @booking.next!
    elsif @booking.update_attributes(params[:booking]) && @booking.aasm_state == "step4"
      redirect_to booking_url(@booking)
      @booking.next!
    end
  end
end

这是新的,重新考虑的更新操作。

  def update
    @booking = Booking.find_by_random_url_key(params[:id])
    @variant = @booking.variant
    if params[:back_button]
      @booking.previous!
      redirect_to :controller => "bookings", :action => "#{@booking.aasm_state}", :id => @booking
    else
      @booking.update_attributes(params[:booking])
      @booking.next!
      redirect_to :controller => "bookings", :action => "#{@booking.aasm_state}", :id => @booking
    end
  end

功能相同,许多行少。并且它不会产生重复的额外夜晚。

谢谢,Rabbot。你的回答让我想到,因为我试图在视图和模型中多次更改内容而没有成功。我只是假设控制器工作,因为它适用于其他一切。