Add role inheritance to attr_accessible #5699

wants to merge 2 commits into


None yet

4 participants


I noticed when making pretty complex applications with a lot of attributes and roles, that the attr_accessible list gets copied a lot, and just isn't very DRY. Adding an inheritance option to attr_accessible enables developers to clearly see the attributes being inherited to the role.

@oscardelben oscardelben commented on the diff Apr 2, 2012
self._accessible_attributes = accessible_attributes_configs.dup
Array(role).each do |name|
- self._accessible_attributes[name] = self.accessible_attributes(name) + args
+ role = inherited_role || name # Use name role if inherited_role doesn't exist
oscardelben Apr 2, 2012

From your example above you pass both :as => :admin and :inherits => :default, but here you are overwriting role if inherited_role was passed. Doesn't that make it meaningless to pass the :as option?

larzconwell Apr 2, 2012

I don't think so, because you still need the :as to access the role. I tested the overwriting in a live application to make sure it didn't mess anything up, and it works perfectly.

larzconwell Apr 2, 2012

Also it's not overwriting any original roles, the Array(role).each is looping through all the given attributes for that role, then if there is an inherited role it will gather those attributes as well, then the + args below will gather the current roles attributes.


I like the idea. Not very much the API though.
What about having something like this :

attr_accessible :title, as: [:admin, :user, :default]

This would avoid adding a new parameter and allow multiple inheritance.


I don't think much can be done with this until after the details on the strong_parameters implementation are hashed out.

I believe the model assignment security is going to be factored out into a gem at some point, IIRC.


@dmathieu Actually that's already available and I didn't know that at the time I wrote this up.

I'll close this since you can already do something similar by just using an array in the as option.

Thanks for the comments!

@vijaydev vijaydev pushed a commit that referenced this pull request Jul 7, 2012
@laknath laknath Changed attr_accessible example to reflect grouped roles
Related to the request #5699 - #5699 and
not documented.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment