使用带有关联的 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 到 false!)
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屋!