如何只在连接表栏中添加记录

时间:2016-02-05 06:47:23

标签: ruby-on-rails many-to-many

我有模型ProgramCoursesStudentprogram coursestudent course ...

之间存在许多关系

首先我添加program's courses然后我向学生展示program courses以便他可以参加课程,但问题是,无论何时我创建学生课程,它都会在课程表中添加课程,我只想添加{联接表中的{1}}和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

3 个答案:

答案 0 :(得分:3)

请尝试以下代码。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/framel"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
     >

    <Button
        android:id="@+id/ChoosePictureButton1j"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="#d3d3d3"
        android:text="CHOOSE PICTURE" 
        >
    </Button>

    <ImageView
        android:id="@+id/CompositeImageViewj"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/truck"
        android:layout_marginTop="50dp"
        >
    </ImageView>

    <ImageView
        android:id="@+id/CompositeImageViewTextj"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_launcher"
        android:layout_marginTop="50dp"
        >
    </ImageView>

    <EditText
        android:id="@+id/edtextj"
        android:layout_width="172dp"
        android:layout_height="62dp"
        android:layout_gravity="bottom"
        android:hint="Enter Caption" />

        <Button
            android:id="@+id/okj"
            android:layout_width="63dp"
            android:layout_height="56dp"
            android:layout_gravity="bottom"
            android:layout_marginBottom=" 5dip"
            android:layout_marginLeft=" 175dip"
            android:background="@drawable/truck" />

        <Button
            android:id="@+id/savej"
            android:layout_width="match_parent"
            android:layout_height="62dp"
            android:layout_gravity="bottom"
            android:layout_marginLeft=" 235dip"
            android:text="SAVE" />
</FrameLayout>

答案 1 :(得分:2)

这条线告诉我们要创建一个全新的课程,并将该课程与学生相关联

@course = @student.courses.create(name:cid) if @student

我相信你的意思是将课程推上学生这样的课程:

@student.courses << course

或者也许,因为看起来你的students_courses表还包含一个name属性而不是简单地拥有连接对象的id,你可能需要这样做

StudentsCourse.create( student: @student, course: @course, name: cid)

上面的代码中可能存在一些错误,但是如果没有看到更多的源代码,很难从问题的呈现方式判断我是否正确连接表的名称。如果你还没有这个,那么这也需要一个连接表的模型。

答案 2 :(得分:2)

您当前的代码效率非常低,如果您只想填充联接,那么您最好使用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 %>
相关问题