ruby-on-railsattr-accessible

Applying attr_accessible to all fields for a given role


I am looking for a way to easily apply attr_accessible to all fields in a model for a given role, so that I can mass assign all fields in my admin console. I'd like to do something like this:

    class User < ActiveRecord::Base
         attr_accessible :name
         attr_accessible :all, :as => :admin
    end

Using :all obviously doesn't work. Is there an easy way I can apply attr_accessible to all fields without having to list them all out, as I have a lot of them, and I don't want to have to remember to do this every time I add a field.


Solution

  • I would implore you to actually take the time to add each field as this offers an opportunity for you to think about whether it actually needs to be attr_accessible. That said, this can be accomplished:

    columns.each do |column|
      attr_accessible column.name.to_sym, :as => :admin
    end