ActiveRecord: How to get all attributes of a model that can be mass-assigned?

Solution 1:

Post.accessible_attributes would cover it if you explicitly defined attr_accessible

Barring, that, doing something like this is clunky but works:

Post.new.attributes.keys - Post.protected_attributes.to_a

Solution 2:

Some of the previously mentioned answers may not apply for Rails 4.

You can use MyModel.attribute_names to get the array of table attributes, although, that might not give you mass assignable attributes, as this aspect of Rails changes with version 4 http://weblog.rubyonrails.org/2012/3/21/strong-parameters/

Solution 3:

For Models you can use MyModel.attribute_names or MyModel.column_names.

For instances you can use MyModel.new.attribute_names.

Solution 4:

Just use

Post.accessible_attributes

That will return all the attributes accessible of the class