【问题标题】:rails, accepts_nested and has_many :through is creating duplicate entriesrails、accepts_nested 和 has_many :through 正在创建重复条目
【发布时间】:2011-02-04 03:28:09
【问题描述】:

我有一个商店模型,其中包含许多具有 has_many :through 关系的产品。

我使用accepts_nested_attributes 进行这项工作,但结果是rails 产生了重复的关联。

我没有什么特别之处,它是一个非常简单的应用程序。

关于为什么要创建重复的员工有什么想法吗?

【问题讨论】:

  • 请发布您目前编写的代码。
  • 让我多做一点,然后我会发布。我只是在检查是否有人知道错误
  • 这是在页面刷新之后出现的吗?
  • @lukas,页面刷新后没有 id。
  • 我将关联更改为 has_many 而不是 :through => 并解决了问题。它正在使用嵌套的嵌套属性创建双重条目。我的代码不是很好,所以我没有在这个问题上添加太多其他内容。你可以忽略它。

标签: ruby-on-rails


【解决方案1】:

看答案:how to avoid duplicates in a has_many :through relationship? 这里:

将 :uniq => true 添加到 has_many :through

class Blog < ActiveRecord::Base
 has_many :blogs_readers, :dependent => :destroy
 has_many :readers, :through => :blogs_readers, :uniq => true
end

class Reader < ActiveRecord::Base
 has_many :blogs_readers, :dependent => :destroy
 has_many :blogs, :through => :blogs_readers, :uniq => true
end

class BlogsReaders < ActiveRecord::Base
  belongs_to :blog
  belongs_to :reader
end

【讨论】:

    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-04
      • 1970-01-01
      • 2012-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-07
      • 1970-01-01
      相关资源
      最近更新 更多