gpt4 book ai didi

ruby-on-rails-3 - 将 Rails Gem Active Admin 与关联一起使用

转载 作者:行者123 更新时间:2023-12-04 07:10:13 24 4
gpt4 key购买 nike

我正在尝试新的 Rails gem http://activeadmin.info/而且效果很好!但是,我找不到有关如何跨协会使用它的任何文档。例如:

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

成员连接表还包括一些额外的数据(即:出勤率)。我正在尝试同时显示类(class)和学生姓名的成员资格 - 并允许对这些姓名进行过滤/排序。据我发现,Active Admin 不能跨关联工作。有没有其他人成功地做到了这一点,或者找到了另一个可以做到的 gem ?非常感谢!

最佳答案

成分.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!)
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 资源中定义表单,覆盖默认值
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

关于ruby-on-rails-3 - 将 Rails Gem Active Admin 与关联一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6024364/

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