Mass assignment in rails3.1
Im working on a simple rails project were users have an attribute called is_admin, and the is_admin cannot be assigned through mass asignment(atr_assecsible). The problem here is, i want to create a default user with the value is_admin => true and i do not no if it would be possible from the migration file because of the protected attribute(:is_admin). so that wen i run rake rails:setup it creates the migrations and the default admi开发者_开发百科n user.
To my knowledge, migrations don't use mass assignment. I don't see why you would have an issue doing this.
If you are using a recent Rails version:
MyModel.create({my_attr: 1, is_admin: true}, without_protection: true)}
or you can look into the as: option
attr_accessible :is_admin, as: :admin
MyModel.create({my_attr: 1, is_admin: true}, as: :admin)}
BTW, without_protection parameter works only on Rails 3.1
精彩评论