[英]how only add record in join table rails
I have model Program
, Courses
and Student
. 我有示范
Program
, Courses
和Student
。 There is many_to_many relationship between program course
and student course
... program course
和student course
之间存在多对多关系...
So first i add program's courses
then i show program courses
to student so that he can take courses but problem is that whenever i create student courses it adds course in course table too i just want to add course_id
and student_id
in join table and don't want to add course in course table because courses are already there.. 因此,首先我添加
program's courses
然后向学生展示program courses
,以便他可以course_id
,但问题是,每当我创建学生课程时,它也将课程添加到课程表中,我只想在连接表中添加course_id
和student_id
而不想要在课程表中添加课程,因为课程已经存在。
my course controller 我的课程负责人
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
Form 形成
<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
Try the following code. 请尝试以下代码。
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
this line is telling rails to create an entirely new course, and associate that course to the student 这条线告诉Rails创建一个全新的课程,并将该课程与学生联系起来
@course = @student.courses.create(name:cid) if @student
i believe you mean to shovel the course onto the students array of courses like this: 我相信您的意思是将课程推向这样的学生课程:
@student.courses << course
or maybe, since it appears your students_courses table also includes a name attribute rather than simply having id's of the joined objects, you may need to do this 也许是因为看起来您的students_courses表还包含一个name属性,而不是简单地具有所连接对象的ID,所以您可能需要这样做
StudentsCourse.create( student: @student, course: @course, name: cid)
there may be some errors in the above code, but without seeing more of your source code it is hard to tell from the way the question is presented whether i have the name of the join table correct. 上面的代码中可能存在一些错误,但是在没有看到更多源代码的情况下,很难通过提出问题的方式来判断问题是否正确显示了联接表的名称。 this will also require a model for the join table, if you don't already have that.
如果还没有联接表,则还需要一个模型。
Your current code is highly inefficient, you'll be better using the collection_singular_ids
method if you want to just populate the join: 您当前的代码效率很低,如果只想填充联接,最好使用
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 %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.