Question

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.

Was it helpful?

Solution 2

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

OTHER TIPS

attr_accessible *column_names, :as => :admin

I was wondering to have column_names instead of column :

(column_names - ['col1', 'col2', 'col3']).each do |column|
  attr_accessible column.to_sym
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top