多个has_many通过相同的关联模型 - activeadmin形式

时间:2013-03-19 10:37:54

标签: ruby-on-rails activeadmin

我有一个叫做游戏的模型。它有两个关联: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

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

但是:当我在游戏模型中更改: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中指定要编辑的模型?

非常感谢!

1 个答案:

答案 0 :(得分: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