# Help with method\_missing in ActiveRecord

**URL:** <https://www.ruby-forum.com/t/help-with-method-missing-in-activerecord/101563>\
**Category:** Rails\
**Created:** [August 6, 2006, 6:34am UTC](https://www.ruby-forum.com/t/help-with-method-missing-in-activerecord/101563 "2006-08-06T06:34:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hammed\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/7feea3/32.png) [@Hammed\_M](https://www.ruby-forum.com/u/Hammed_M)\
**Post date:** [August 6, 2006, 6:34am UTC](https://www.ruby-forum.com/t/help-with-method-missing-in-activerecord/101563/1 "2006-08-06T06:34:24Z")

</div>

I am serializing a data object (FooData) into an ActiveRecord column  
(Foo).

I’d like to be able to do delegate methods to the data class if the Foo  
class doesn’t have that attribute. So instead of:

f = Foo.new  
f.data.item1

I’d like to do:

f = Foo.new  
f.item1

I was hoping that I could add a method\_missing method to my Foo class  
and  
call methods in the FooData class but I’m not sure how to go about doing  
that. In my example below, I get Stack Level too deep errors.

class Foo \< ActiveRecord::Base  
serialize :data, FooData

def after\_initialize  
self.data = eval("#{self.class}Data.new") unless self.data  
end

def method\_missing(method\_name, \*args)  
self.data.send “#{method\_name}(#{args})”  
super  
end  
end

class FooData  
attr\_accessor item1, item2  
end

---

<div class="post-metadata">

**Author:** ![Hammed\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/7feea3/32.png) [@Hammed\_M](https://www.ruby-forum.com/u/Hammed_M)\
**Post date:** [August 6, 2006, 6:46am UTC](https://www.ruby-forum.com/t/help-with-method-missing-in-activerecord/101563/2 "2006-08-06T06:46:24Z")

</div>

Hi~

On Aug 5, 2006, at 9:34 PM, Hammed M. wrote:

> f = Foo.new  
> f.item1
> 
> I was hoping that I could add a method\_missing method to my Foo  
> class and call methods in the FooData class but I’m not sure how to  
> go about doing that. In my example below, I get Stack Level too  
> deep errors.

class Foo \< ActiveRecord::Base  
serialize :data, FooData

def after\_initialize  
self.data = eval("#{self.class}Data.new") unless self.data  
end

def method\_missing(method\_name, \*args, &blk)  
if self.data.respond\_to? method\_name  
self.data.send(method\_name, args, &blk)  
else  
super  
end  
end

class FooData  
attr\_accessor item1, item2  
end

```
The reason you version is overflowing the stack is because you call

```

super even if your method\_missing works. ActiveRecord uses  
method\_missing to get columns so you always want to call super in  
your method\_missing def’s, but only if you don’t do something with  
the method. So here you check if your FooData.respond\_to? whatever  
method invoked method\_missing. And then if FooData doesn;'t respond  
to that method we call super instead to let the next method\_missing  
pick up the method if it wants to.

Cheers-  
-Ezra

---

<div class="post-metadata">

**Author:** ![Hammed\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/7feea3/32.png) [@Hammed\_M](https://www.ruby-forum.com/u/Hammed_M)\
**Post date:** [August 6, 2006, 7:28am UTC](https://www.ruby-forum.com/t/help-with-method-missing-in-activerecord/101563/3 "2006-08-06T07:28:26Z")

</div>

Hi Ezra,

> The reason you version is overflowing the stack is because you call  
> super even if your method\_missing works.

Excellent! Thanks Ezra.

I had to add another condition to method\_missing because self.data  
results  
in another method\_missing call which calls self.data which…

def method\_missing(method\_name, \*args, &blk)  
if !method\_name.eql?(:data) and self.data.respond\_to? method\_name  
self.data.send(method\_name, \*args, &blk)  
else  
super  
end  
end

This is really cool - I can now pretend the methods in the data class  
are  
part of the ActiveRecord class. Thanks again.

Hammed
