简体   繁体   中英

ActiveRecord : Cannot modify association because it goes through more than one other association

I'm not sure why this association is invalid

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

Then I'd expect to be able to do the following

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

except this throws the error

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

What is the fundamental error here?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM