如何在rails中更新多对多关系的属性?

时间:2013-07-11 04:41:47

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 ruby-on-rails-3.2

我是铁杆新手。所以我不太理解它。我有一个有三个表的数据库。学生表,课程表和注册表。 我的Railde模型如下:

class Student < ActiveRecord::Base
  attr_accessible :name
  has_many :registrations
  has_many :courses, :through => :registrations

  validates :name, :presence => true

end

class Course < ActiveRecord::Base
  attr_accessible :name
  has_many :registrations, :dependent => :destroy
  has_many :students, :through => :registrations

  validates :name , :presence => true
  validates :name, :uniqueness => true
end

class Registration < ActiveRecord::Base
  attr_accessible :course_id, :student_id


  belongs_to :course
  belongs_to :student

  validates :course_id, :student_id, :presence => true
  validates :course_id, :uniqueness => {:scope => :student_id}
  validates :student_id, :uniqueness => {:scope => :course_id}
end

......................

Controller action for updating student :

  def update
    @student = Student.find(params[:id])
    if @student.update_attributes(params[:student])
      redirect_to students_path, :notice => "Registration completed"
    else
      render 'edit'
    end
  end

................ 查看:

<%=form_for @student do |f| %>
    <p>
      <%= f.label :name, "Name" %>
      <%= f.text_field :name %>
    </p>
    <p>
      <%= render('course_list') %>
    </p>
    <p>
      <%= f.submit %>
    </p>
<% end %>

............... _course_list partial:

Select Courses :<br/>

        <p>

          <% Course.all.each do |course| %>

              <%=check_box_tag "student[course_ids][]", course.id, `enter code here`@student.course_ids.include?(course.id) %>
              <%= course.name %>  <br/>

          <% end %>
    </p>

............................. 当我提交更新按钮时,我收到了错误

无法批量分配受保护的属性:course_ids

.......

参数:

{"utf8"=>"✓",
 "_method"=>"put",
 "authenticity_token"=>"j/lDE5bv1gWkfadQ6Cag6hGjg5nD2Ikad9vHOJTE7Pc=",
 "student"=>{"name"=>"Galib",
 "course_ids"=>["2",
 "3"]},
 "commit"=>"Update Student",
 "id"=>"6"} 

.........................

我想要做的是,如果点击更新按钮,则需要更新学生表和注册表。请帮忙。

3 个答案:

答案 0 :(得分:3)

在模型中定义关联

class Student < ActiveRecord::Base
  attr_accessible :name
  has_many :registrations
  has_many :courses, :through => :registrations

  validates :name, :presence => true

  accepts_nested_attributes_for :courses
  attr_accessible :course_ids

end

class Course < ActiveRecord::Base
  attr_accessible :name
  has_many :registrations, :dependent => :destroy
  has_many :students, :through => :registrations

  validates :name , :presence => true
  validates :name, :uniqueness => true

  accepts_nested_attributes_for :students
end

class Registration < ActiveRecord::Base
  attr_accessible :course_id, :student_id


  belongs_to :course
  belongs_to :student

  validates :course_id, :student_id, :presence => true
  validates :course_id, :uniqueness => {:scope => :student_id}
  validates :student_id, :uniqueness => {:scope => :course_id}

  accepts_nested_attributes_for :course
end

在控制器

def update
    @student = Student.find(params[:id])
    if @student.update_attributes(params[:student])
       @student.courses.build
      redirect_to students_path, :notice => "Registration completed"
    else
      render 'edit'
    end
  end

它可能对你有帮助

答案 1 :(得分:0)

对于MongoDB,要在rails模型中实现多对多,您必须使用has_and_belongs_to_many而不是简单has_many

答案 2 :(得分:0)

错误告诉您您有权限错误 - 无法从表单发布course_ids。更具体地说,您在学生模型中有attr_accessible :name,这意味着当您使用表单保存学生记录时,这是唯一可以保存的属性(attr_accessible指示可以批量分配的内容)。

尝试将学生型号更改为:

attr_accessible :name, :registrations_attributes
accepts_nested_attributes_for :registrations

您正在尝试创建嵌套的模型表单,因此使用accepts_nested_attributes_for

如果您想要更新Registrations表,那么您将不得不告诉partial,以便它知道partial正在更新与Student不同的模型。

<%= f.fields_for :registrations do |registration| %>
  <%= render('course_list') %>
<% end %>