使用Rails Gem Active Admin和关联 [英] Using Rails Gem Active Admin with Associations
问题描述
我正在尝试使用新的Rails gem http://activeadmin.info/ ,它的效果非常好!不过,我找不到关于如何在关联中使用它的任何文档。例如:
I'm trying out the new Rails gem http://activeadmin.info/ and it's working great! However I can't find any documentation on how to use it across associations. For example:
class Membership < ActiveRecord::Base
belongs_to :course
belongs_to :person
class Course < ActiveRecord::Base
has_many :memberships
has_many :people, :through => :memberships
class Person < ActiveRecord::Base
has_many :memberships
has_many :courses, :through => :memberships
成员资格连接表也包含一些额外数据(即:出勤率)。我试图向会员显示课程名称和学生姓名 - 并允许对这些名称进行过滤/排序。据我所知,Active Admin并不适用于各种关联。有没有其他人成功地做到这一点,或找到另一个宝石呢?非常感谢!
The membership join table includes some extra data as well (ie: attendance). I'm trying to show the membership with both the course and student name - and allow filtering / sorting on those names. As far as I have found, Active Admin doesn't work across associations. Has anyone else been successful in doing that, or found another gem that does? Thanks so much!
推荐答案
ingredient.rb
class Ingredient < ActiveRecord::Base
has_and_belongs_to_many :products, :join_table => :ingredients_products
end
product.rb
class Product < ActiveRecord::Base
has_and_belongs_to_many :ingredients, :join_table => :ingredients_products
end
不要忘记加入表的迁移(:id )
don't forget the migrations for the joining table (:id to false!)
class CreateProductsIngredients < ActiveRecord::Migration
def self.up
create_table :ingredients_products,:id => false do |t|
t.integer :product_id
t.integer :ingredient_id
t.timestamps
end
end
def self.down
drop_table :products_ingredients
end
end
现在在你的ActiveAdmin中定义表单资源,重写默认值
Now define the form in you ActiveAdmin resource, override the default
ActiveAdmin.register Product do
form do |f|
f.inputs "Details" do
f.input :product_name
f.input :brand
f.input :ingredients # don't forget this one!
end
end
这篇关于使用Rails Gem Active Admin和关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!