【问题标题】:multiple has_many through with same associated model - activeadmin form具有相同关联模型的多个 has_many - activeadmin 表单
【发布时间】:2013-03-19 10:37:53
【问题描述】:

我有一个模型叫做游戏。它有两个关联:with :categorytoptens 和 :categorygames:

has_many :categorytoptens
has_many :categories, :through => :categorytoptens, :dependent => :destroy

has_many :categorygames
has_many :categories, :through => :categorygames, :dependent => :destroy

要编辑类别,我将其添加到我的 activeadmin games.rb 文件中:

    f.input :categories, :as => :check_boxes, :collection => Category.all, :member_label => :navititle_de

它显示类别模型中所有类别的复选框列表,并将选择保存在 categorygames 中。所以,一切正常。

但是:当我在游戏模型中更改 :categorytoptens 和 :categorygames 的顺序时,类别会保存在 categorytoptens 中:

has_many :categorygames
has_many :categories, :through => :categorygames, :dependent => :destroy

has_many :categorytoptens
has_many :categories, :through => :categorytoptens, :dependent => :destroy

我的问题:

a.) 是否“允许”多个 has_many 具有相同的模型关联?模型关联是否相互“覆盖”? b.) 有没有办法在 activeadmin 中指定要编辑的模型?

非常感谢!

【问题讨论】:

    标签: ruby-on-rails activeadmin


    【解决方案1】:

    你不应该定义两个具有相同名称的 has_many 关联!永远不要尝试重命名您的关联

    has_many :categorygames
    has_many :games_categories, :through => :categorygames, :dependent => :destroy
    
    has_many :categorytoptens
    has_many :top_ten_categories, :through => :categorytoptens, :dependent => :destroy
    

    A) 您可以定义两个具有相同名称但后继的关联并覆盖前一个。为每个关联尝试一些好听的名称(uniq)

    B) 需要更多信息,您想在活动管理员中管理资源吗?

    试试

    ActiveAdmin.register YourResourceName do
    end
    

    您也可以为您的资源设置自定义名称,点击此链接http://activeadmin.info/docs/2-resource-customization.html#rename_the_resource

    【讨论】:

    • 非常感谢,就像一个魅力。最后我不得不添加“:source =>”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-14
    • 2017-01-06
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多