# Looking up accessor methods for ActiveRecord objects

**URL:** <https://www.ruby-forum.com/t/looking-up-accessor-methods-for-activerecord-objects/143531>\
**Category:** Rails\
**Created:** [July 20, 2008, 2:44am UTC](https://www.ruby-forum.com/t/looking-up-accessor-methods-for-activerecord-objects/143531 "2008-07-20T02:44:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![seatown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dfb087/32.png) [@seatown](https://www.ruby-forum.com/u/seatown)\
**Post date:** [July 20, 2008, 2:44am UTC](https://www.ruby-forum.com/t/looking-up-accessor-methods-for-activerecord-objects/143531/1 "2008-07-20T02:44:35Z")

</div>

Hi,

I am trying to invoke a method on an ActiveRecord object using something  
like:

k = :date=  
obj.method(k).call(new\_date)

Initially obj.method(k) is throwing a NameError. However, if I first  
explicitly invoke _any_ accessor method, then the above works as  
expected:

obj.date # just get the date  
k = :date=  
obj.method(k).call(new\_date) # works now

Is there a good way to ensure that these accessor methods are loaded  
before  
I try looking them up with .method()?

Thanks,  
Erik

---

<div class="post-metadata">

**Author:** ![seatown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dfb087/32.png) [@seatown](https://www.ruby-forum.com/u/seatown)\
**Post date:** [July 20, 2008, 3:03am UTC](https://www.ruby-forum.com/t/looking-up-accessor-methods-for-activerecord-objects/143531/2 "2008-07-20T03:03:12Z")

</div>

> Is there a good way to ensure that these accessor methods are loaded  
> before  
> I try looking them up with .method()?

obj.public\_methods.include(‘k’)

? (Also I can’t remember if k must be a symbol - :k)

BTW I doubt your premise, because I thought all attribute accessors get  
generated at .find time…

---

<div class="post-metadata">

**Author:** ![seatown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dfb087/32.png) [@seatown](https://www.ruby-forum.com/u/seatown)\
**Post date:** [July 20, 2008, 3:41am UTC](https://www.ruby-forum.com/t/looking-up-accessor-methods-for-activerecord-objects/143531/3 "2008-07-20T03:41:14Z")

</div>

Hi –

On Sun, 20 Jul 2008, Erik R. wrote:

> explicitly invoke _any_ accessor method, then the above works as  
> expected:
> 
> obj.date # just get the date  
> k = :date=  
> obj.method(k).call(new\_date) # works now

Interestingly, that will happen when you call any unknown method on  
the object:

> > t  
> > =\> #\<Team id: 277401923, name: “Persuaders”, mascot: “Cat”, … \>  
> > t.methods.grep(/mascot/)  
> > =\> []  
> > t.blah  
> > NoMethodError: undefined method `blah’ for #Team:0x242e2d8  
> > t.methods.grep(/mascot/)  
> > =\> [“mascot”, “mascot=”, “mascot?”]

method\_missing creates the column attributes immediately if they don’t  
exist, no matter what. The same thing happens with respond\_to?

> Is there a good way to ensure that these accessor methods are loaded  
> before  
> I try looking them up with .method()?

You could call define\_attribute\_methods on the class. That’s basically  
what method\_missing does.

David

–  
Rails training from David A. Black and Ruby Power and Light:  
Intro to Ruby on Rails July 21-24 Edison, NJ  
Advancing With Rails August 18-21 Edison, NJ  
See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![seatown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dfb087/32.png) [@seatown](https://www.ruby-forum.com/u/seatown)\
**Post date:** [July 20, 2008, 3:43am UTC](https://www.ruby-forum.com/t/looking-up-accessor-methods-for-activerecord-objects/143531/4 "2008-07-20T03:43:35Z")

</div>

Hi –

On Sat, 19 Jul 2008, Phlip wrote:

> > Is there a good way to ensure that these accessor methods are loaded  
> > before  
> > I try looking them up with .method()?
> 
> obj.public\_methods.include(‘k’)

The column attributes aren’t there initially. (Try it; you’ll see.)

> ? (Also I can’t remember if k must be a symbol - :k)
> 
> BTW I doubt your premise, because I thought all attribute accessors get  
> generated at .find time…

No, find doesn’t define the column attribute methods.

David

–  
Rails training from David A. Black and Ruby Power and Light:  
Intro to Ruby on Rails July 21-24 Edison, NJ  
Advancing With Rails August 18-21 Edison, NJ  
See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!
