具有has_many命名空间模型的资源的Active Admin

时间:2014-12-11 12:04:02

标签: ruby-on-rails activeadmin nested-attributes

我有一个V1::CategoryName模型

class V1::CategoryName < ActiveRecord::Base
  has_many :categories, :class_name => 'V1::Category', dependent: :delete_all, inverse_of: :category_name
  has_many :brand_names, :class_name => 'V1::BrandName', dependent: :delete_all, inverse_of: :category_name

  validates :name, :uniqueness => true

  accepts_nested_attributes_for :brand_names, allow_destroy: true
end

我有一个V1::BrandName模型

class V1::BrandName < ActiveRecord::Base
  has_many :brands, :class_name => 'V1::Brand', dependent: :delete_all
  belongs_to :category_name, :class_name => 'V1::CategoryName'

  validates :category_name, :uniqueness => {:scope => [:category_name, :name]}
  validates :category_name, presence: true
end

admin/v1_category_name.rb

ActiveAdmin.register V1::CategoryName do
  permit_params :name, :variants => [], brand_names: [ :name ]

  index do
    column :name
    column :variants
    column :brands do |b|
      # print brand names, maybe comma separated
    end
    actions
  end

  show do |cs|
    attributes_table do
      row :brand_names do |c|
        c.has_many :brand_names do |b| # wrong
          b.name
        end
      end
    end
  end

  form do |f|
    f.inputs "Brands" do
      f.has_many :brand_names, heading: false, allow_destroy: true do |b| # error
        b.input :name
      end
    end
    f.actions
  end
end

但我收到错误Could not find the inverse association for category_name (:brand_name in V1::CategoryName)

当我点击尝试编辑条目时。

2 个答案:

答案 0 :(得分:0)

你试过这个吗?

class V1::BrandName < ActiveRecord::Base
  ...
  belongs_to :category_name, :class_name => 'V1::CategoryName', inverse_of: :brand_names
  ...
end

答案 1 :(得分:0)

我认为问题是对clases的命名错误

class V1::BrandName < ActiveRecord::Base   
  has_many :brands, :class_name => 'V1::brand', dependent: :delete_all   
  belongs_to :category_name, :class_name => 'V1::category_name'

  validates :category_name, :uniqueness => {:scope => [:category_name, :name]}   
  validates :category_name, presence: true end

class V1::CategoryName < ActiveRecord::Base
  has_many :categories, :class_name => 'V1::Category', dependent: :delete_all, inverse_of: :category_name
  has_many :brand_names, :class_name => 'V1::Brand_Name', dependent: :delete_all, inverse_of: :category_name

  validates :name, :uniqueness => true

  accepts_nested_attributes_for :brand_names, allow_destroy: true
end