views:

208

answers:

1

I would like to have a list of all attribute names that can be mass assigned. I need this for a custom form builder that will not add input fields by default that cannot be mass assigned. For example if I have a model like :

class Post < ActiveRecord::Base
  attr_protected :account

  belongs_to :author

  validates_presence_of :title, :author
end

I would like to have as a result a list containing :author, :title.

Any idea if such a method exists? Or some guidelines on how I can make one that does this?

Thanks!

+5  A: 

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
semanticart
Thanks @semanticart for your answer!
Vincent
so, if accessible_attributes gets the ones that are explicitly attr_accessible'd and protected_attributes gets the ones that are explicitly attr_protect'd, is there a method that will tell you which ones are accessible regardless of whether they are protected through attr_protected or attr_accessible ?
Chris Drappier