首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Rails设计模型

用Rails设计模型
EN

Stack Overflow用户
提问于 2016-06-18 09:27:43
回答 1查看 79关注 0票数 0

我有以下几种型号:

  • 研究所
  • 学生
  • 教室
  • 课程

Requirements:

  1. 一个教室可以有许多课程。一门课程可以属于许多教室。
  2. 一个教室可以有许多学生。一个学生可以属于许多教室。
  3. 一个学生可以选修许多课程。一门课程可以属于许多学生。
  4. 一个学院有许多学生,许多教室和许多课程。

我试图为上面的内容创建关联:下面的内容是否准确?

  1. 学院: has_and_belongs_to_many :学生会has_many :教室has_many :课程
  2. 学生: has_and_belongs_to_many :研究所
  3. 教室: has_and_belongs_to_many :学生
  4. 课程: has_and_belongs_to_many :学生会has_many :教室belongs_to :研究所

我怎么才能在这里使用“贯穿”关系呢?

EN

回答 1

Stack Overflow用户

发布于 2016-06-18 11:20:35

Classroom关联和Cource关联做类似的操作

跑:

代码语言:javascript
复制
ruby homework.rb

#homework.rb
require 'active_record'

`rm foobar.db`

class User < ActiveRecord::Base
  establish_connection adapter: 'sqlite3', database: 'foobar.db'
  connection.create_table table_name, force: true do |t|
    t.string :name
  end
  connection.create_table :institutes_users, id: false do |t|
    t.integer :user_id
    t.integer :institute_id
  end

  has_and_belongs_to_many :institutes
end

class Institute < ActiveRecord::Base
  establish_connection adapter: 'sqlite3', database: 'foobar.db'
  connection.create_table table_name, force: true do |t|
    t.string :name
  end
  connection.create_table :institutes_students, id: false do |t|
    t.integer :student_id
    t.integer :institute_id
  end

  has_and_belongs_to_many :users
  has_and_belongs_to_many :students
  has_many :classrooms
  has_many :courses
end

class Student < ActiveRecord::Base
  establish_connection adapter: 'sqlite3', database: 'foobar.db'
  connection.create_table table_name, force: true do |t|
    t.string :name
  end
  connection.create_table :classrooms_students, id: false do |t|
    t.integer :student_id
    t.integer :classroom_id
  end

  has_and_belongs_to_many :institutes
  has_and_belongs_to_many :classrooms
  has_many :courses
end

class Classroom < ActiveRecord::Base
  establish_connection adapter: 'sqlite3', database: 'foobar.db'
  connection.create_table table_name, force: true do |t|
    t.string :name
    t.references :institute
  end

  belongs_to :institute
  has_and_belongs_to_many :students
end

class Course < ActiveRecord::Base
  establish_connection adapter: 'sqlite3', database: 'foobar.db'
  connection.create_table table_name, force: true do |t|
    t.string :name
    t.references :institute
    t.references :student
  end

  belongs_to :institute
  belongs_to :student
end
user = User.create!(name: 'John')
institute = Institute.create!(name: 'Harvard')
student = Student.create(name: 'Mary')
institute.users << user
institute.classrooms.create(name: '4th floor room')
institute.courses.create(name: 'IT')
institute.students << student
student.classrooms << Classroom.create(name: '5th froor room')
student.courses << Course.create(name: 'Biology')
p institute.users
p institute.classrooms
p institute.courses
p institute.students
p student.classrooms
p student.courses
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37895402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档