模型结构中的多对多关系

时间:2015-02-01 14:06:16

标签: ruby-on-rails ruby

我有两个模型学生和学院,我希望能够列出学生申请的学院以及申请大学的学生名单。

我有这个表格,可以创建一个学生,并通过复选框

选择大学
<div class="row">

  <div class="col-md-6 col-md-offset-3">


      <h3>Add Students</h3>
     <%= simple_form_for @student do |f| %>
  <%= f.input :name %>
  <%= f.input :sex, collection: ["Male", "Female"] %>
  <%= f.input :age, collection: 15..100 %>
  <%= f.input :dob %> 
  <%= f.input :current_school %>
  <%= f.input :current_level %>
  <%= f.input :country,  priority: [ "Singapore" ] %>
  <%= f.input :sat_score %>
  <%= f.input :applied_colleges, collection: College.all,group_method: :id, as: :check_boxes %>    <%= f.input :current_mentor, collection: Mentor.all, group_method: :name, as: :select %> 
  <%= f.button :submit %>

<% end %>
  </div>

这是应用的架构:

ActiveRecord::Schema.define(version: 20150131123428) do

create_table "colleges", force: true do |t|
  t.string   "name"
  t.string   "country"
  t.integer  "sat_min_score"
  t.integer  "sat_max_score"
  t.integer  "tution_fees"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.integer  "student_list"
end

create_table "mentors", force: true do |t|
  t.string   "name"
  t.string   "sex"
  t.integer  "age"
  t.date     "dob"
  t.text     "bio"
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "students", force: true do |t|
  t.string   "name"
  t.string   "sex"
  t.integer  "age"
  t.date     "dob"
  t.string   "current_school"
  t.string   "current_level"
  t.string   "country"
  t.integer  "sat_score"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.integer  "current_mentor_id"
  t.string   "applied_colleges"
  t.string   "current_mentor"
end

 add_index "students", ["current_mentor_id"], name:       "index_students_on_current_mentor_id"

end

这是学生模特

class Student < ActiveRecord::Base
  serialize :applied_colleges, Array
  has_one :mentor
  validates :name, presence: true, length: { maximum:50 }
  validates :age, presence: true, numericality: { lesser_than_or_equal_to: 120, only_integer: true}
  validates :sat_score, presence: true, numericality: {lesser_than_or_equal_to: 1600, only_integer: true}

  def self.college_id_to_college_name(applied_colleges)
      colleges = []
      applied_colleges.each do |var|
      colleges << var.to_i
     end
    @college_list = []
    colleges.each do |var|
    @college_list << College.find_by(id:var)
      end
    @college_list.delete_at(@college_list.length-1)
    @college_list
  end

    def self.save_applied_student_id_to_student_joined_column_for_college(student_id,college_list)
   college_list.each do |college|
   college.student_list << student_id
     end
   end
end

这是大学模式

    class College < ActiveRecord::Base
    validates :name, presence:true, length: { maximum:50 }
    validates :sat_min_score, presence:true, numericality: {lesser_than: 2400, only_integer: true}
    validates :sat_max_score, presence:true, numericality: {lesser_than_or_equal_to: 2400, only_integer: true}
    end

我对学生和大学之间的模型非常困惑。模型需要这样,每个学生对象都有他申请的大学列表,每个大学都有申请的学生名单。

目前我正在做的是保存学生已申请的大学列表,方法是通过表格中的复选框保存大学的ID,并将其保存在专栏中。

我正在考虑在名为student_list的列中插入每个学生的学生ID。但这似乎是一种糟糕的方式。

我想以这样一种方式为学生和学院建模,即每个学生对象都有他申请的大学名单,每个大学对象都有申请的学生名单。

我该如何建模?需要什么样的迁移?

1 个答案:

答案 0 :(得分:0)

如果你想有一个很好的方式加入学生和学院,我会制作一个名为Application的模型[小心,这可能是一个保留字,所以你可能需要一个不同的模型名称 - 不要回想起我的头脑](需要一个student_id和一个college_id)。学生has_many应用程序,has_many大学通过应用程序。反向学院,有很多应用程序,有很多学生通过申请。申请属于学生,且属于大学。

虽然在这一点上,你正在混合学生和申请人的传统定义。一般来说,人们可能是申请人,申请人可能是注册学生。

相关问题