【问题标题】:has_many through can not modify associationhas_many 通过不能修改关联
【发布时间】:2014-08-19 12:26:15
【问题描述】:

我有三个模型:Almon、Bela 和 Lingo

class Almon < ActiveRecord::Base

  belongs_to :bela, dependent: :destroy
  has_many :lingos, through: :bela, dependent: :delete_all

  accepts_nested_attributes_for :bela
  accepts_nested_attributes_for :lingos, :allow_destroy => true
end

我正在尝试在 Almon 中提交嵌套表单,但我得到了

无法修改关联“Almon#Lingos”,因为源反射类“Lingo”通过 :has_many 关联到“Bela”。

谁能告诉我我做错了什么?

【问题讨论】:

  • 请出示您的代码
  • 现在够清楚了吗?

标签: ruby-on-rails ruby forms nested


【解决方案1】:

您需要使用 has_many 而不是 belongs_to,如下所示:

has_many :belaes, dependent: :destroy
has_many :lingoes, through: :belaes, dependent: :delete_all

【讨论】:

  • 好的,这意味着我必须重组模型。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多