2013-05-26 75 views
2

建立数据库,我只是好奇,如果我做得正确,因为它看起来有点关闭。有些人拥有用户帐户和角色(教师或学生)。他们是一个班的参与者(一个班有很多学生和老师,一个学生有很多班,一个班有很多班)。我想我的class_instruction模式是关闭的DB,但请告诉我,如果它会工作,或是否有更好的方法(如可能与参与者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 

回答

1

我会做一个既有班级又有人的连接表,并且有很多通过使用。如果人们有很多班级,班级中有很多人,那么你不能以其他方式做到这一点。

+0

我不认为你的用户角色需要使用有很多通过。除非用户既可以是老师也可以是学生。 –

1

我看到平时belongs_to的用单数形式:

belongs_to :person 

我不知道这是否是你所要求的不过。