gpt4 book ai didi

ruby-on-rails-3 - Rails 管理中的 ActiveRecord::HasManyThroughNestedAssociationsAreReadonly 错误

转载 作者:行者123 更新时间:2023-12-03 15:02:18 25 4
gpt4 key购买 nike

我刚刚升级到 Rails 3.2.10并且收到一条错误消息,这是我在通过 RailsAdmin 更新记录时从未收到过的。

ActiveRecord::HasManyThroughNestedAssociationsAreReadonly at /admin/vendor/12/edit

Message Cannot modify association 'Vendor#categories' because it goes through more than one other association.

这是我的 Vendor模型:
class Vendor < ActiveRecord::Base
attr_accessible :name, :description, :banner_image, :logo_image, :intro_text, :thumb_image, :category_ids, :product_ids, :user_id, :remove_banner_image, :banner_image_cache, :remove_logo_image, :logo_image_cache
mount_uploader :banner_image, ImageUploader
mount_uploader :logo_image, ImageUploader
mount_uploader :thumb_image, ImageUploader

has_many :products, :dependent => :destroy
has_many :categories, :through => :products
belongs_to :owner, :class_name => "User",
:foreign_key => "user_id"
end

这是我的 Category模型:
class Category < ActiveRecord::Base
attr_accessible :name, :product_ids, :category_ids
has_many :category_products do
def with_products
includes(:product)
end
end

has_many :products, :through => :category_products

end

这是我的 Product模型:
class Product < ActiveRecord::Base
attr_accessible :name, :description, :price, :vendor_id, :image, :category_ids, :sku, :remove_image, :image_cache
mount_uploader :image, ImageUploader

belongs_to :vendor
has_many :category_products do
def with_categories
includes(:category)
end
end

has_many :categories, :through => :category_products

end

这是我的 CategoryProduct模型:
class CategoryProduct < ActiveRecord::Base
attr_accessible :product_id, :category_id, :purchases_count

belongs_to :product
belongs_to :category

validates_uniqueness_of :product_id, :scope => :category_id
end

最佳答案

发生这种情况是因为您的关联是嵌套的,意思是(来自 rails 源):
如果有多个连接表,则嵌套直通关联……这就是您的情况。

显然,一种解决方法(我没有测试)告诉供应商它不需要自动保存关联。

has_many :categories, :through => :products, :autosave => false

关于ruby-on-rails-3 - Rails 管理中的 ActiveRecord::HasManyThroughNestedAssociationsAreReadonly 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14187248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com