class_instruction中的学生和老师

时间:2013-05-26 16:24:17

标签: ruby-on-rails ruby-on-rails-3

设置数据库,我只是好奇,如果我做得正确,因为它看起来有点偏。有些人拥有用户帐户和角色(教师或学生)。他们是一个班级的参与者(一个班级有很多学生和老师;一个学生有很多班级;一个老师有很多班级)。我认为我的class_instruction模型在数据库中是关闭的,但请告诉我它是否可行,或者是否有更好的方法(例如可能有一个has_many_through参与者表)

schema.rb:

ActiveRecord::Schema.define(:version => 20130524160107) do

  create_table "class_instructions", :force => true do |t|
    t.string   "name"
    t.datetime "time"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
    t.integer  "person_id"
  end

  add_index "class_instructions", ["person_id"], :name => "index_class_instructions_on_person_id"

  create_table "people", :force => true do |t|
    t.string   "firstName"
    t.string   "lastName"
    t.integer  "user_id"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

  add_index "people", ["user_id"], :name => "index_people_on_user_id"

  create_table "roles", :force => true do |t|
    t.string   "name"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

  create_table "user_roles", :force => true do |t|
    t.integer  "user_id"
    t.integer  "role_id"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

  add_index "user_roles", ["role_id"], :name => "index_user_roles_on_role_id"
  add_index "user_roles", ["user_id"], :name => "index_user_roles_on_user_id"

  create_table "users", :force => true do |t|
    t.string   "email",                  :default => "", :null => false
    t.string   "encrypted_password",     :default => "", :null => false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          :default => 0
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
  end

  add_index "users", ["email"], :name => "index_users_on_email", :unique => true
  add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true

end

我担心的是person_id是班级的一部分。这是对的吗?

ClassInsturction.rb:

class ClassInstruction < ActiveRecord::Base
  belongs_to :people
  has_many :cassignments
  has_many :assignments, :through => :cassignments

  def className
    self.name
  end

  def classAssignments
    return self.cassignments
  end
end

2 个答案:

答案 0 :(得分:1)

我通常看到belongs_to以单数形式出现:

belongs_to :person

我不确定这是否是你所要求的。

答案 1 :(得分:1)

我会制作一个既有班级又有人的加入表,并且使用了很多。如果人们有很多班级,班级有很多人,你就不能以其他方式做到这一点。

相关问题