我有模型Program
,Courses
和Student
。有program course
和student course
之間MANY_TO_MANY關係......如何只添加記錄中加入臺導軌
所以首先我添加program's courses
我要讓program courses
學生這樣他就可以參加課程,但問題是,每當我創造學生的課程它當然表格中新增當然也只是我要添加course_id
和student_id
連接表,不希望添加課程課程表,因爲課程已經有..
我當然控制器
def create
@program = Program.find(params[:program_id]) if params[:program_id]
@course = @program.courses.create(program_course_params) if @program
@student = Student.find(params[:student_id]) if params[:student_id]
if params[:student_id]
params[:student].each do |cid|
@course = @student.courses.create(name:cid) if @student
end
end
end
形式
<table>
<% @student.program.courses.each do |c| %>
<tr><td><%=c.name %><td>
<td><%= check_box_tag "student[]",c.name,:name %></td>
</tr>
<% end %>
<tr>
</table>
class Course < ActiveRecord::Base
has_and_belongs_to_many :students
has_and_belongs_to_many :programs
end
class Program < ActiveRecord::Base
has_and_belongs_to_many :courses
has_many :students
end
class Student < ActiveRecord::Base
has_and_belongs_to_many :courses
belongs_to :program
end
是什麼PARAMS [:學生]:含有到底是什麼? – Dheeresha
課程名稱 –
編輯我的問題 –