ruby-on-rails - class_instruction 中的学生和教师

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

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

架构.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 是类(class)的一部分。这是正确的吗?

类指令.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

最佳答案

我会制作一个既有类又有人员的连接表,并且用途有很多。如果人们有很多类(class),并且类(class)有很多人,那么您就无法以其他方式做到这一点。

关于ruby-on-rails - class_instruction 中的学生和教师,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16761239/

相关文章:

ruby-on-rails - 无法在Rails中验证CSRF token 的真实性

ruby-on-rails-3 - 在 Heroku 上运行 Yard 服务器

ruby-on-rails - rails : How to enter value A in Model X only if value A exists in Model Y?

jquery - 如何使用 jQuery 添加和删除表单字段?

ruby-on-rails - Rails ActionView::Template::Error 未定义方法 `count' 解析器

ruby-on-rails - Ruby on Rails : 2. 3.8,过滤器不起作用之前的自定义?

css - Rails 没有编译 application.scss 中的 css 文件?

ruby-on-rails - ActiveRecord::HasManyThroughOrderError

ruby-on-rails-3 - cancan Skip_authorization_check 用于设计身份验证

ruby-on-rails-3 - Rails 回形针和倒置图像