【问题标题】:duplicate rows in join table with has_many => through and accepts_nested_attributes_for使用 has_many => through 和 accept_nested_attributes_for 在连接表中重复行
【发布时间】:2010-06-16 02:50:41
【问题描述】:

一个事件有很多艺术家,一个艺术家有很多事件。艺术家和活动的结合模型是表演。我想将艺术家添加到活动中。

除了在创建新事件时我的联接表中出现重复条目​​之外,这可行。这会导致其他地方出现问题。

事件.rb

validates_presence_of :name, :location

has_many :performances, :dependent => :destroy
has_many :artists, :through => :performances

accepts_nested_attributes_for :artists, :reject_if => proc {|a| a['name'].blank?}
# accepts_nested_attributes_for :performances, :reject_if => proc { |a| a['artist_id'].blank? }

艺术家.rb

has_many :performances
has_many :events, :through => :performances

has_attached_file :photo, :styles => { :small => "150x150>"  },
              :url  => "/images/artists/:id/:style/:basename.:extension",
              :path => ":rails_root/public/images/artists/:id/:style/:basename.:extension"

# validates_attachment_presence :photo
validates_attachment_size :photo, :less_than => 5.megabytes
validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']

validates_presence_of :name

has_many :mixes  

性能.rb

belongs_to :artist
belongs_to :event

events_controller.rb

def new
  @event = Event.new
  @event.artists.build

  respond_to do |format|
    format.html # new.html.erb
    format.xml  { render :xml => @event }
  end
end

def create
  @event = Event.new(params[:event])

  respond_to do |format|
    if @event.save
      flash[:notice] = 'Event was successfully created.'
      format.html { redirect_to(admin_events_url) }
      format.xml  { render :xml => @event, :status => :created, :location => @event }
    else
      format.html { render :action => "new" }
      format.xml  { render :xml => @event.errors, :status => :unprocessable_entity }
    end
  end
end

_form.html.erb

<% form_for([:admin,@event]) do |f| %>
<p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
</p>
<p>
    <%= f.label :location %><br/>
    <%= f.text_field :location %>
</p>
<p>
    <%= f.label :date %><br />
    <%= f.date_select :date %>
</p>
<p>
    <%= f.label :description %><br />
    <%= f.text_area :description %>
</p>
<% f.fields_for :artists do |builder| %>
    <p>   
        <%= builder.label :name, "Artist"%><br/>
        <%= builder.text_field :name %><br/>
    </p>
<% end %>
<p>
    <%= f.submit 'Submit' %> <%= link_to 'Cancel', admin_events_path %>
</p>
<% end %>

第二个 Accept_nested_attributes_for 的输出被注释掉了

Processing Admin::EventsController#create (for 127.0.0.1 at 2010-06-15 21:10:24) [POST]
Parameters: {"commit"=>"Submit", "authenticity_token"=>"KigiyUNIE2iYTwo59lf7SClbG9Dxge7WEWDDd08OLEc=", "event"=>{"name"=>"test event", "artists_attributes"=>{"0"=>{"name"=>"test artist"}}, "date(1i)"=>"2010", "location"=>"some location", "date(2i)"=>"6", "date(3i)"=>"16", "description"=>"blah"}}
User Columns (2.4ms)   SHOW FIELDS FROM `users`
User Load (0.3ms)   SELECT * FROM `users` WHERE (`users`.`id` = 13) LIMIT 1
Event Columns (1.2ms)   SHOW FIELDS FROM `events`
Artist Columns (1.4ms)   SHOW FIELDS FROM `artists`
SQL (0.1ms)   BEGIN
Event Create (0.3ms)   INSERT INTO `events` (`name`, `created_at`, `location`, `updated_at`, `date`, `description`) VALUES('test event', '2010-06-16 04:10:24', 'some location', '2010-06-16 04:10:24', '2010-06-16', 'blah')
Artist Create (0.2ms)   INSERT INTO `artists` (`name`, `created_at`, `photo_file_size`, `updated_at`, `photo_file_name`, `photo_content_type`, `photo_updated_at`, `bio`) VALUES('test artist', '2010-06-16 04:10:24', NULL, '2010-06-16 04:10:24', NULL, NULL, NULL, NULL)
[paperclip] Saving attachments.
Performance Columns (1.1ms)   SHOW FIELDS FROM `performances`
Performance Create (0.2ms)   INSERT INTO `performances` (`event_id`, `artist_id`) VALUES(10, 22)
Performance Create (0.1ms)   INSERT INTO `performances` (`event_id`, `artist_id`) VALUES(10, 22)
SQL (0.4ms)   COMMIT
Redirected to http://localhost:3000/admin/events
Completed in 97ms (DB: 8) | 302 Found [http://localhost/admin/events]

【问题讨论】:

  • 你的参数是什么样的?您可以在运行 WEBrick 的终端中看到它们。
  • 添加了 event.rb 和 artist.rb 的完整代码,我忽略了这些代码,因为我相信这些行与手头的问题无关。但我可能是错的。 (验证、回形针和与其他模型的关系)

标签: ruby-on-rails


【解决方案1】:

我刚刚使用您提供的代码创建了一个快速测试 Rails 应用程序,结果如下:

Processing EventsController#create (for 192.168.1.2 at 2010-06-16 00:33:05) [POST]
    Parameters: {"commit"=>"Create", "authenticity_token"=>"R8lKqeTIbRQ5Ft8K+TNMNusCh4qmnOv8xxSIi25MMNE=", "event"=>{"name"=>"Event", "artists_attributes"=>{"0"=>{"name"=>"Me"}}, "date(1i)"=>"2010", "location"=>"nowhere", "date(2i)"=>"6", "date(3i)"=>"16", "description"=>"Test Event"}}
      Event Create (0.4ms)   INSERT INTO "events" ("name", "location", "created_at", "updated_at", "date", "description") VALUES('Event', 'nowhere', '2010-06-16 04:33:05', '2010-06-16 04:33:05', '2010-06-16', 'Test Event')
      Artist Create (12.3ms)   INSERT INTO "artists" ("name", "created_at", "updated_at") VALUES('Me', '2010-06-16 04:33:05', '2010-06-16 04:33:05')
      Performance Create (0.2ms)   INSERT INTO "performances" ("name", "created_at", "event_id", "updated_at", "artist_id") VALUES(NULL, '2010-06-16 04:33:05', 1, '2010-06-16 04:33:05', 1)
    Redirected to http://192.168.1.5:3000/events/1
    Completed in 251ms (DB: 13) | 302 Found [http://192.168.1.5/events]

您运行的是什么版本的 Rails?

您是否尝试过创建一个干净的测试应用来查看它是否有效?

可能存在导致问题的 gem 或某些插件。我正在使用 SQLite3 运行 2.3.8。

我没有得到您显示的所有额外输出,这可能是 MySQL 特定的与 has_many :through 关系相关的内容,但这也值得关注。

这是我的架构供参考:

  create_table "artists", :force => true do |t|
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "events", :force => true do |t|
    t.string   "name"
    t.string   "location"
    t.date     "date"
    t.text     "description"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "performances", :force => true do |t|
    t.string   "name"
    t.integer  "artist_id"
    t.integer  "event_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

【讨论】:

  • 添加了完整的输出并注释掉了第二个 Accepts_nested_attributes_for。结果似乎是一样的。
  • @shalako:我认为应该可以解决它。
  • 很好,但看起来我的代码是正确的(正如你所建议的那样),我只是将它复制到 stackoverflow 错误。 :(
  • 这里更新了 Artists.rb,确认我的代码是正确的,再次运行测试。更新的输出。相同的行为。
  • @shalako:我更新了我的答案。我现在要睡觉了。我明天再回来看看事情进展如何。祝你好运! :)
【解决方案2】:

通过将 Rails 更新到最新版本 2.3.8 解决了这个问题。这似乎是 2.3.5 中的一个错误。

https://rails.lighthouseapp.com/projects/8994/tickets/3659-accepts_nested_attributes_for-causes-duplicate-entries-in-join-model-table

【讨论】:

  • 嘿!我很高兴听到你修好了它。如果我的“您正在运行什么版本的导轨?”对您有所帮助?问题,请考虑投票我的问题或接受它作为答案。当您只回答问题时,代表似乎真的很难获得=)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-16
  • 1970-01-01
  • 2013-04-22
  • 1970-01-01
  • 1970-01-01
  • 2012-03-09
相关资源
最近更新 更多