Active Record DB建模 - Ruby on Rails

时间:2017-01-27 14:31:14

标签: mysql ruby-on-rails ruby

好吧,伙计们,这可能有点令人困惑,所以坚持下去。让我先介绍一下我的表格。基本上我在迁移时创建了四个表。

  • 学生
  • 教师
  • 受试者
  • 管理员用户

以下是我的迁移文件:

生:

def up
    create_table :students, :id => false do |t|
      t.integer "student_id",:primary_key => true
      t.string "first_name", :limit => 25
      t.string "last_name", :limit => 50
      t.string "email", :default => ' ', :null => false
      t.string "birthday"
      t.string "subjects"
      t.string "teachers"
      t.string "username", :limit => 25
      t.string "password_digest"
      t.timestamps
    end

教师:

def up
    create_table :teachers, :id => false do |t|
      t.integer "teacher_id", :primary_key => true
      t.string "first_name"
      t.string "last_name"
      t.string "email", :default => ' ', :null => false
      t.string "birthday"
      t.string "subjects"
      t.string "username", :limit => 25
      t.string "password_digest"
      t.timestamps
    end

主题:

def up
  create_table :subjects, :id => false do |t|
    t.integer "subject_id", :primary_key => true
    t.string "subject_name"
    t.timestamps
  end
end

管理员用户:

def up
  create_table :admin_users, :id => false do |t|
    t.integer "admin_user_id", :primary_key => true
    t.string "username", :limit => 25
    t.string "password_digest"
    t.timestamps
  end
end

现在让我解释一下。基本上是:

  • 一名学生可以有很多老师
  • 一位老师可以有很多学生
  • 一名学生可以有很多科目
  • 一位老师可以教授很多科目
  • 管理员可以访问所有这些内容(创建,编辑,删除)

我创建了这些字段并将其设置在我的模型上:

class Student < ApplicationRecord

  has_and_belongs_to_many :subjects
  has_and_belongs_to_many :teachers
  has_many :admin_users
  has_secure_password
  self.primary_key = :student_id

end

class Teacher < ApplicationRecord

  has_and_belongs_to_many :subjects
  has_and_belongs_to_many :students
  has_many :admin_users
  has_secure_password

end

class Subject < ApplicationRecord

  has_and_belongs_to_many :students
  has_and_belongs_to_many :teachers
  has_many :admin_users

  # scope :search, lambda { |query| where(["name LIKE ?", "%#{query}%"])}

end

class AdminUser < ApplicationRecord

  has_secure_password

  scope :newest_first, lambda { order("created_at ASC") }
  scope :oldest_first, lambda { order("created_at DESC") }
  # scope :search, lambda { |query| where(["name LIKE ?", "%#{query}%"])}
end

现在我手动在mysql数据记录中插入数据,然后尝试为学生的科目和教师表单字段提取记录。

如何有效地管理我的数据库,尤其是教师,学生和科目?有什么我需要先做的关联我的表吗?请帮忙。对不起,很长的帖子。我是新手。感谢你的解释(外行的术语)和答案。

检查我的模特。我是否需要创建一个单独的表来关联教师,学生和科目?

旁注:当我拉起我的学生和教师字段时,它会给我一个错误ActiveRecord_Associations_CollectionProxy:0x007f9c504361d0错误。

1 个答案:

答案 0 :(得分:3)

我认为这种关系适用于你的情况:

生:

def up
  create_table :students, :id => false do |t|
    t.integer "student_id",:primary_key => true
    t.string "first_name", :limit => 25
    t.string "last_name", :limit => 50
    t.string "email", :default => ' ', :null => false
    t.string "birthday"
    t.string "subjects"
    t.string "username", :limit => 25
    t.string "password_digest"
    t.timestamps
  end
end

参考:Generating auto increment with sequence 1001 教师:

def up
    create_table :teachers, :id => false do |t|
      t.integer "teacher_id", :primary_key => true
      t.string "first_name"
      t.string "last_name"
      t.string "email", :default => ' ', :null => false
      t.string "birthday"
      t.string "subjects"
      t.string "username", :limit => 25
      t.string "password_digest"
      t.timestamps
    end
end

主题:

def up
  create_table :subjects, :id => false do |t|
    t.integer "subject_id", :primary_key => true
    t.string "subject_name"
    t.timestamps
  end
end

注册科目:

def up
  create_table :enrolled_subjects, :id => false do |t|
    t.integer "subject_id"
    t.integer "teacher_id"
    t.integer "student_id"
  end
end

型号:

class Student < ApplicationRecord
  has_many :enrolled_subjects
  has_many :subjects, through: :enrolled_subjects
  has_many :teachers, through: :enrolled_subjects    

  def teacher_names
    self.teachers.map(&:first_name).join(", ")
  end
end

class Teacher < ApplicationRecord    
  has_many :enrolled_subjects
  has_many :subjects, through: :enrolled_subjects
  has_many :students, through: :enrolled_subjects
end

class Subject < ApplicationRecord
  has_many :enrolled_subjects
  has_many :students, through: :enrolled_subjects
  has_many :teachers, through: :enrolled_subjects
end

class EnrolledSubject < ActiveRecord::Base
  belongs_to :student, foreign_key: :student_id
  belongs_to :subject, foreign_key: :subject_id
  belongs_to :teacher, foreign_key: :teacher_id
end

Example Repository

相关问题