Rails 管理中的 ActiveRecord::HasManyThroughNestedAssociationsAreReadonly 错误 [英] ActiveRecord::HasManyThroughNestedAssociationsAreReadonly Error in Rails Admin

查看:48
本文介绍了Rails 管理中的 ActiveRecord::HasManyThroughNestedAssociationsAreReadonly 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I just upgraded to Rails 3.2.10 and am getting an error message that I never used to get when updating a record via RailsAdmin.

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

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

这是我的供应商模型:

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

这是我的产品模型:

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_admin 将不会为供应商生成表单中的类别字段:

You can mark the association as readonly and rails_admin will then not generate the category fields in the form for vendor:

has_many :categories, ->{ readonly },通过::products

这篇关于Rails 管理中的 ActiveRecord::HasManyThroughNestedAssociationsAreReadonly 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆