0

你能解释一下我如何从我在 Rails 应用程序中创建的关联中删除重复项吗?该应用程序允许用户教授不同的课程,以及学习不同的课程。可供学习的课程与可供教授的课程相同。出于这个问题的目的,假设有 3 门课程可以教授和学习,science, math, english

我目前拥有关联的方式如下。我创建了一个 Target 模型(代表可供学习的课程)并播种了三门课程,science, math, english. 我还创建了一个像这样的课程模型(代表用户正在教授的课程)并播种了相同的三门课程。请注意,如果不清楚,我将模型称为“目标”,因为它代表用户尝试学习的主题。

User 

has_and_belongs_to_many :targets #courses the user is studying 

has_and_belongs_to_many :courses #courses that users are teaching

因此,我相信我有一些不必要的重复。Course 和 Target 模型将具有完全相同的数据(即相同的课程)。有没有办法创建一个模型(代替两个 Target.rb 和 Course.rb)并用它来代表用户正在教授和学习的课程?

更新

为了帮助澄清,我只是参考种子数据来帮助解释/显示模型在做什么,即 Target.rb 和 Course.rb 本质上是相同的,只是扮演不同的角色。它更像是用户和目标/课程之间未来关系的模板。例如,我可能正在学习科学但教数学。

4

2 回答 2

1

听起来你有课程。

class Course < ActiveRecord::Base
  attr_accessible :subject
  has_and_belongs_to_many :students
  belongs_to :teacher
end

在这种设计中,一个课程可以有很多学生注册,一个学生可以注册任意数量的课程。每门课程可以指定一名教师。一门课程将有一个(字符串)属性,称为主题,它是名称,如“数学”或“英语”。课程模型可以扩展为包含额外的信息,如教室编号、难度、长度等。

这将允许像这样的查询

alice = Student.find_by_first_name("Alice")
alice.courses.last.subject
=> "english"
alice.courses.last.teacher.first_name
=> "Eric"

或者

eric = Teacher.find_by_first_name("Eric")
eric.courses.where(subject: 'math').count
=> 3

这表明埃里克正在教三门数学课。

于 2013-08-11T01:32:43.750 回答
0

下面的解决方案。您确定要在学生和课程之间建立 one_to_many 连接吗 - 这似乎是明显的多对多。无论如何,重要的一点是第二联想。

class User

  has_and_belongs_to_many :courses   #or possibly better has_many :through, your call
  has_many :taught_courses, :class_name => :course, :foreign_key => teacher_id

end

class Course
  belongs_to :teacher, :class_name => :user, :foreign_key => teacher_id
  has_and_belongs_to_many :users
end

更新:

上面的代码将创建两个关联。第一个是 habtm 关联,它需要另一个名为 courses_users 的表,其中包含两列(course_id 和 user_id,没有 id 列)。这会给你:

course.users       #=> list of users taking the course

如果你愿意,你可以重命名这个关联写作:

has_and_belongs_to_many :students, :class_name => :user

第二个关联是 one_to_many,因此您可以通过以下方式使用它:

course.teacher         #=> return single user or nil
user.taught_courses    #=> return list of courses taught by given user

另一个更新:

通过两个表进行多个 habtm 关联(您可能更喜欢一个表,但是您需要使用 has_many :through,无论如何在这种情况下这可能会更好。架构:

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

create_table "courses_students", :id => false do |t|
  t.integer "course_id"
  t.integer "user_id"
end

create_table "courses_teachers", :id => false do |t|
  t.integer "course_id"
  t.integer "user_id"
end

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

楷模:

class Course < ActiveRecord::Base
  attr_accessible :name

  has_and_belongs_to_many :teachers, :class_name => "User", :join_table => :courses_teachers
  has_and_belongs_to_many :students, :class_name => "User", :join_table => :courses_students
end

class User < ActiveRecord::Base
  attr_accessible :name

  has_and_belongs_to_many :courses, :join_table => :courses_students
  has_and_belongs_to_many :taught_corses, :join_table => :courses_teachers
end
于 2013-08-11T01:27:11.017 回答