gpt4 book ai didi

ruby-on-rails - Rails Active Admin 多对多关系

转载 作者:数据小太阳 更新时间:2023-10-29 08:40:12 25 4
gpt4 key购买 nike

我有以下型号

class Owner < ActiveRecord::Base
has_many :business_owners
has_many :businesses, :through => :business_owner
end

class Business < Active Record::Base
has_many :business_owners
has_many :businesses, :through => :business_owner
end

class BusinessOwner < ActiveRecord::Base
belongs_to :owners
belongs_to :businesses
end

我已尝试将 BusinessOwner 模型添加到管理仪表板:

rails generate active_admin:resource BusinessOwner

它在 app/admin/中创建一个名为 business_owners 的文件当我尝试查看企业主时,我收到以下错误:

uninitialized constant BusinessOwner::Owners
Extracted source (around line #1):

1: render renderer_for(:index)

谁能告诉我如何在多对多关系中使用 active admin?

最佳答案

你的人际关系看起来不对。

belongs_to 关系中,Rails 希望您使用单数形式

class BusinessOwner < ActiveRecord::Base
belongs_to :owner
belongs_to :business
end

同样,您需要正确引用:through

  has_many :businesses, :through => :business_owners

(即复数所有者)

通常值得在考虑 ActiveAdmin 之前启动 Rails 控制台(或编写测试,呵呵)来测试这些东西;)

关于ruby-on-rails - Rails Active Admin 多对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9450988/

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