Rails Association挑战赛

时间:2012-02-25 20:02:16

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

遇到一些挑战,需要跟踪员工所采取的课程。到目前为止,我就是这些表格

Course: course_id,name,category_of_course...#holds Course related details

Employee: employee_id, name...#holds Employee details

progress:course_id,employee_id, status ...#holds which course have been 
taken and by whom

在我的模特中,我有这种关系:

 Employee: has_many :courses

请问如何使用员工未参加的课程填充选择列表(假设在注册课程后,它被标记为已采用)。我使用rails 3.0.9与MySql。 谢谢

1 个答案:

答案 0 :(得分:1)

您需要定义

belongs_to :employee
belongs_to :course

(在您的进度模型中)

然后您可以定义

#Employee
has_many :progresses
has_many :courses, :through => :progresses

#Course
has_many :progresses
has_many :employees, :through => :progresses

选择列表将是:

= form_for @something do |f|
  = f.select :course_id, Course.all.select{|x| not @employee.courses.include?(x)}.collect{|x|[x.name,x.id]} #Ideally this logic should reside in the model.