1

我是新的鐵路。所以我不理解它。我有一個有三個表的數據庫。學生表,課程表和註冊表。 我Railde型號如下:如何更新rails中多對多關係的屬性?

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部分:

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> 

............................. 當我提交更新按鈕時,出現錯誤

Can'質量t指派保護屬性:course_ids

.......

參數:

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

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

我想要什麼做的是,如果點擊更新按鈕,則需要更新學生表和註冊表。請幫忙。

回答

0

在MongoDB中的情況下,對於軌道模型實現多對多你必須使用has_and_belongs_to_many,而不是簡單的has_many

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 

它可能會幫助你

0

錯誤告訴你,你有一個權限錯誤 - course_ids無法從表單發佈到。更具體地說,您在學生模型中有attr_accessible :name,這意味着這是您使用表單保存學生記錄時可以保存的唯一屬性(attr_accessible指示可以批量分配的內容)。

試着改變你的學生模型:

attr_accessible :name, :registrations_attributes 
accepts_nested_attributes_for :registrations 

您正在試圖創建一個嵌套的模型形式,所以accepts_nested_attributes_for用於此。

如果您還希望更新註冊表,那麼您將必須告訴部分,以便它知道部分正在更新與學生不同的模型。

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