Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make all model attributes accessible for mass-assignment?

I have made all attributes in a rails app not accessible using this application configuration option:

config.active_record.whitelist_attributes = true

In most cases I define a few attributes I want to be accessible as accessible using attr_accessible in models. How do I make all attributes of a particular model accessible. Something like attr_accessible :all.

like image 796
serengeti12 Avatar asked Jan 22 '12 15:01

serengeti12


2 Answers

You can make all attributes accessible by calling attr_protected without arguments like that:

class User < ActiveRecord::Base
  # roughly speaking sets list of model protected attributes to []
  # making all attributes accessible while mass-assignment
  attr_protected
end
like image 86
KL-7 Avatar answered Oct 14 '22 21:10

KL-7


I've found this approach more readable:

class User < ActiveRecord::Base
  attr_accessible *column_names
end

Changing config.active_record.whitelist_attributes will affect all your models, whereas this will only apply to the one model.

The attr_protected way also works, but I find it confusing (since it's doing the opposite of what it seems to say at first glance).

like image 13
Dave Guarino Avatar answered Oct 14 '22 20:10

Dave Guarino