ActiveRecord:无法修改关联,因为它通过多个其他关联

时间:2017-10-09 12:39:32

标签: ruby-on-rails activerecord associations

我不确定为什么这种关联无效

class Tag < ActiveRecord::Base
  has_and_belongs_to_many :routes
end

class Route < ActiveRecord::Base
 belongs_to :super_route
 has_and_belongs_to_many :tags
end

class SuperRoute < ActiveRecord::Base
  has_one :route
  has_many :tags, through: :route
end

然后我希望能够做到以下

s = SuperRoute.create
s.route = Route.create
s.tags << Tag.create

除了抛出错误

ActiveRecord::HasManyThroughNestedAssociationsAreReadonly: Cannot modify association 'SuperRoute#tags' because it goes through more than one other association.

这里的根本错误是什么?

1 个答案:

答案 0 :(得分:0)

class SuperRoute < ActiveRecord::Base
  has_one :route
  has_many :tags     #remove through: :route
end