Active Admin嵌套模型与现有数据

时间:2014-12-27 02:17:33

标签: ruby-on-rails nested activeadmin

我正在使用ActiveAdmin,我想使用嵌套模型。 我有一个小组和学生模特,一个小组有很多学生。 我想要做的是当我创建一个组时,能够在系统中添加现有学生,并且能够动态地使用数据表来搜索它们。 我已经拥有的东西:

class Group < ActiveRecord::Base
 has_many :students
 accepts_nested_attributes_for :students
end

class Student < ActiveRecord::Base
 belongs_to     :group
end

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

创建一个连接表: -

rails g migration groups_students group_id:integer student_id:integer

class GroupsStudents < ActiveRecord::Migration
  def change
    create_table :groups_students, :id => false do |t|
      t.integer :group_id
      t.integer :student_id
    end
  end
end 

在群组模型中: -

attr_accessible :student_ids
has_and_belongs_to_many :students
accepts_nested_attributes_for :students

在学生模型中: -

has_and_belongs_to_many :groups

查看Active admin的文档并将Group模型注册为: -

rails generate active_admin:resource Group

在app / admin / groups.rb中: -

form do |f|
 # f.semantic_errors
  f.inputs "Groups" do
  f.input :table_fields # To DO :: Add other fields

  f.input :students, as: :check_boxes, collection: Student.all     
end
f.actions