Rails を使用して、学校の生徒とコースの関係に関するプロジェクトを実行したいと考えています。学生は多くのコースを持つことができ、コースは多くの学生を持つことができます。したがって、学生とコースの関係は「多対多」だと思います
私はコンソールに行きました:
u = User.first
User Load (0.7ms) SELECT "users".* FROM "users" LIMIT 1
...
c = Course.first
Course Load (0.8ms) SELECT "courses".* FROM "courses" LIMIT 1
...
UserCourseship.create( :user => u, :course => c )
次に、私が得たエラーは次のとおりです。
ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes:user, course
これが私のuser.rb
class User < ActiveRecord::Base
has_many :user_courseships
has_many :courses, :through => :user_courseships
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
user.provider = auth.provider
user.uid = auth.uid
user.name = auth.info.name
user.oauth_token = auth.credentials.token
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
user.save!
end
end
def facebook
@facebook ||= Koala::Facebook::API.new(oauth_token)
end
end
じぶんのcourse.rb
class Course < ActiveRecord::Base
has_many :user_courseships
has_many :users, :through => :user_courseships
attr_accessible :name, :sn, :time
end
そしてその関係user_courseship.rb
class UserCourseship < ActiveRecord::Base
belongs_to :user
belongs_to :course
attr_accessible :course_id, :user_id
end