如何只在 join table rails 中添加记录
how only add record in join table rails
我有模型 Program
、Courses
和 Student
。 program course
和 student course
...
之间存在 many_to_many 关系
所以首先我添加 program's courses
然后我向学生显示 program courses
以便他可以上课但问题是每当我创建学生课程时它也会在课程 table 中添加课程我只想在 join table 中添加 course_id
和 student_id
,不想在课程 table 中添加课程,因为课程已经存在了..
我的课程控制器
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
试试下面的代码。
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_to_add = Course.find_by_id(cid)
@course = @student.courses << course_to_add unless course_to_add.blank?
end
end
end
这一行告诉 rails 创建一门全新的课程,并将该课程关联到学生
@course = @student.courses.create(name:cid) if @student
我相信你的意思是将课程铲到学生的课程数组中,如下所示:
@student.courses << course
或者,由于您的 students_courses table 似乎还包含一个名称属性,而不是简单地具有连接对象的 ID,您可能需要这样做
StudentsCourse.create( student: @student, course: @course, name: cid)
上述代码中可能存在一些错误,但在没有看到更多源代码的情况下,很难从提出问题的方式判断我的连接名称是否正确 table。这还需要一个连接模型 table,如果您还没有的话。
您当前的代码非常低效,如果您只想填充连接,最好使用 collection_singular_ids
方法:
#config/routes.rb
resources :programs do
resources :students do
match :courses, via: [:get, :put] #-> url.com/programs/:program_id/students/:student_id/courses
end
#app/controllers/students_controller.rb
class StudentsController < ApplicationController
def courses
@program = Program.find params[:program_id]
@student = Student.find params[:student_id]
@courses = Course.all
@student.update course_id_params if request.put?
end
private
def course_id_params
params.require(:student).permit(course_ids: [])
end
end
#app/views/students/courses.html.erb
<%= form_for @student do |f| %>
<%= f.collection_check_boxes :course_ids, @courses, :id, :name %>
<%= f.submit %>
<% end %>
我有模型 Program
、Courses
和 Student
。 program course
和 student course
...
所以首先我添加 program's courses
然后我向学生显示 program courses
以便他可以上课但问题是每当我创建学生课程时它也会在课程 table 中添加课程我只想在 join table 中添加 course_id
和 student_id
,不想在课程 table 中添加课程,因为课程已经存在了..
我的课程控制器
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
试试下面的代码。
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_to_add = Course.find_by_id(cid)
@course = @student.courses << course_to_add unless course_to_add.blank?
end
end
end
这一行告诉 rails 创建一门全新的课程,并将该课程关联到学生
@course = @student.courses.create(name:cid) if @student
我相信你的意思是将课程铲到学生的课程数组中,如下所示:
@student.courses << course
或者,由于您的 students_courses table 似乎还包含一个名称属性,而不是简单地具有连接对象的 ID,您可能需要这样做
StudentsCourse.create( student: @student, course: @course, name: cid)
上述代码中可能存在一些错误,但在没有看到更多源代码的情况下,很难从提出问题的方式判断我的连接名称是否正确 table。这还需要一个连接模型 table,如果您还没有的话。
您当前的代码非常低效,如果您只想填充连接,最好使用 collection_singular_ids
方法:
#config/routes.rb
resources :programs do
resources :students do
match :courses, via: [:get, :put] #-> url.com/programs/:program_id/students/:student_id/courses
end
#app/controllers/students_controller.rb
class StudentsController < ApplicationController
def courses
@program = Program.find params[:program_id]
@student = Student.find params[:student_id]
@courses = Course.all
@student.update course_id_params if request.put?
end
private
def course_id_params
params.require(:student).permit(course_ids: [])
end
end
#app/views/students/courses.html.erb
<%= form_for @student do |f| %>
<%= f.collection_check_boxes :course_ids, @courses, :id, :name %>
<%= f.submit %>
<% end %>