# ActiveRecord dynamic attribute methods

**URL:** <https://discuss.rubyonrails.org/t/activerecord-dynamic-attribute-methods/56977>\
**Category:** rubyonrails-talk\
**Created:** [January 26, 2011, 4:52pm UTC](https://discuss.rubyonrails.org/t/activerecord-dynamic-attribute-methods/56977 "2011-01-26T16:52:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tariff\_Pricking](https://avatars.discourse-cdn.com/v4/letter/t/9fc29f/32.png) [@Tariff\_Pricking](https://discuss.rubyonrails.org/u/Tariff_Pricking)\
**Post date:** [January 26, 2011, 4:52pm UTC](https://discuss.rubyonrails.org/t/activerecord-dynamic-attribute-methods/56977/1 "2011-01-26T16:52:20Z")

</div>

in a project we are enforcing permissions by wrapping active record objects and arrays of ar objects in a proxy class. this proxy class is a BasicObject that uses #method\_missing to send methods to a target (the ar object) i believe this is how :has\_many association/reflection works (at least in 2.x).

i wanted to benchmark this technique against simply extending ar objects on the fly with a permissions module. now i realize extending objects create entirely new singleton classes but i just wanted to test this method out.

i thought i could simply over-write the #read\_attribute and #write\_attribute methods and enforce permissions that way but it seems not all dynamic attribute methods utilize these methods. all attribute setter methods look like they are routed through #write\_attribute, but not all getter methods are routed through #read\_attribute - as far as i can tell only datetime getters are routed through #read\_attribute. all other getter methods touch the @attributes instance variable directly.

does anyone know why the dynamic getter attribute methods behave this way? wouldn't it be prudent to have one end-all-be-all method that all read attribute methods go through? (excluding before type casting lookups)

---

<div class="post-metadata">

**Author:** ![Tariff\_Pricking](https://avatars.discourse-cdn.com/v4/letter/t/9fc29f/32.png) [@Tariff\_Pricking](https://discuss.rubyonrails.org/u/Tariff_Pricking)\
**Post date:** [January 27, 2011, 5:36pm UTC](https://discuss.rubyonrails.org/t/activerecord-dynamic-attribute-methods/56977/2 "2011-01-27T17:36:19Z")

</div>

after looking deeper in ActiveModel i found that all dynamically generated attribute methods are included in a dynamic anonymous module. it can be accessed from an active record class via .generated\_attribute\_methods (User.generated\_attribute\_methods)

i was eventually able to alias all generated methods and set up a permission chain upon object extension - this method works but seems very HEAVY. would be nicer if all attribute manipulation went through #read\_attribute and #write\_attribute.
