简体   繁体   中英

attr_accessible <-> :as => :admin <-> db:seed <->

I am sort of in a catch22 situation.

  1. If have to protect the 'role' attribute for a user from mass assignment problems I have to use attr_accessible and exclude it.
  2. But then if I have to allow an admin to set it then I have to add :as => :admin using assign_attributes.
  3. But then db:seed won't work if I have to setup some default roles.

Is there a way to accomplish all three needs?

Thank you.

There are a few ways around mass assignment but I'm not really a fan of them.

For example:

role = Role.new
role.assign_attributes({...}, :without_protection => true)
role.save

I prefer to use a gem I wrote called sudo_attributes that gives you "sudo" privileges:

Role.sudo_create(...)

The API is identical to ActiveRecord's for creating and instantiating models.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM