【问题标题】:Cannot modify association because the source reflection class is associated via :has_many无法修改关联,因为源反射类是通过 :has_many 关联的
【发布时间】:2013-09-06 07:39:40
【问题描述】:

我在 Rails 2 项目中的关联如下:

class Role < ActiveRecord::Base
  belongs_to :user
  has_many :attrs, :through => :user, :dependent => :destroy
end

class User < ActiveRecord::Base
  has_many :roles, :order => 'id DESC', :dependent => :destroy
  has_many :attr, :dependent => :destroy
end

class Attr < ActiveRecord::Base
  belongs_to :user
  has_many :roles, :through => :user
end

我正在将我的项目升级到 rails 3。在尝试删除 rails 3 中的用户时,我收到以下错误:

无法修改关联“Role#attrs”,因为源反射类“Attr”通过 :has_many 关联到“User”。

但同样的事情,在 Rails 2 中,用户可以正常删除。我从未在 Rails 2 上工作过,所以我不得不在这里问。

我可以做些什么来解决这个问题?我检查了其他答案,但它们似乎不适用于我的情况。

【问题讨论】:

    标签: activerecord ruby-on-rails-3.2 ruby-on-rails-2


    【解决方案1】:

    在用户模型中 has_many :attr, :dependent => :destroy

    应该是 has_many :attrs, :dependent => :destroy

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多