我在课程和用户之间有一个 has_many through 关系。
class Course < ActiveRecord::Base
belongs_to :user
has_many :enrollments, :dependent => :delete_all
has_many :users, :through => :enrollments
attr_accessible :description, :duration, :name, :prerequisites, :short_name, :start_date, :user_id
accepts_nested_attributes_for :users, :allow_destroy => true
attr_accessible :users_attributes
和用户:
class User < ActiveRecord::Base
has_many :subjects, :class_name => "Course" # to get this call user.subjects
has_many :enrollments, :dependent => :delete_all
has_many :courses, :through => :enrollments
和注册:
class Enrollment < ActiveRecord::Base
belongs_to :course
belongs_to :user
attr_accessible :course_id, :user_id
end
现在我正在尝试使用嵌套表单从 Course 内部设置 user_ids。它一直给我那个 Mass Assignment 警告,没有任何东西被保存。我读到我应该添加 attr_accessible user_id 但它仍然不起作用。
即使我从 rails 控制台做这样的事情:
@c.update_attributes({:user_ids => [7,8]})
@c 是课程
任何帮助将不胜感激,谢谢。