Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails协会挑战赛_Ruby On Rails_Ruby On Rails 3_Activerecord - Fatal编程技术网

Ruby on rails Rails协会挑战赛

Ruby on rails Rails协会挑战赛,ruby-on-rails,ruby-on-rails-3,activerecord,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

面临一些挑战,需要跟踪员工所学的课程。到目前为止,这些桌子都是这样的

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
请问我如何用员工尚未参加的课程填充选择列表(假设在注册课程后,该课程被标记为已参加)。我在MySql中使用rails 3.0.9。 谢谢

您需要定义

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.

谢谢,尝试了此操作,但出现了一些错误[undefined method`course_id'for#],将:course_id更改为:id可为我获取课程,但它们在选择列表中都表示为#。这是再次生成的感谢,解决了名称问题(尽管我仍然得到未定义的错误,不得不使用:id而不是:course\u id…),但在“注册”课程后,它仍然出现在选择列表中。注册课程时,我只需将课程id、员工id插入progress表,我知道我做错了什么,请你再指出一点正确的方向好吗?听起来这些关系并不像我刚才提到的那样。通过Rails控制台检查您是否能够通过
某个员工查看课程。课程
刚刚执行此操作,它会按预期返回员工注册的课程。感谢您花时间添加信息,在将一些记录插入表中之后,我添加了我的课程id和员工id列,这些表上的PK是默认的rails id列。