# Method\_missing, respond\_to? and AR

**URL:** <https://www.ruby-forum.com/t/method-missing-respond-to-and-ar/143304>\
**Category:** Rails\
**Created:** [July 17, 2008, 3:41am UTC](https://www.ruby-forum.com/t/method-missing-respond-to-and-ar/143304 "2008-07-17T03:41:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wawaloo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/bc8723/32.png) [@wawaloo](https://www.ruby-forum.com/u/wawaloo)\
**Post date:** [July 17, 2008, 3:41am UTC](https://www.ruby-forum.com/t/method-missing-respond-to-and-ar/143304/1 "2008-07-17T03:41:24Z")

</div>

Hi,

I have a class Page with attribute ‘title’. I want Project objects to  
call Page methods when they don’t find the called method. If the method  
is not found whithin Page, super is called. For example, I can write:

p = Project.first  
p.title #which calls page.title (it works)

Here is my code:

class Project \< AR::Base  
def method\_missing(method\_name, \*args)  
if page.respond\_to?(method\_name)  
a = page.send(method\_name, \*args)  
elsif respond\_to?(method\_name)  
a = send(method\_name, \*args)  
else  
super  
end  
return a  
end  
end

It works fine with p.title. But if I do this:

> > p.respond\_to :title\_before\_type\_cast  
> > =\> true
> 
> > p.title\_before\_type\_cast  
> > I get a SystemStackError…

I don’t understand why respond\_to? returns true AND  
p.send(:title\_before\_type\_cast) calls method\_missing at the same time?

Thanks!

---

<div class="post-metadata">

**Author:** ![wawaloo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/bc8723/32.png) [@wawaloo](https://www.ruby-forum.com/u/wawaloo)\
**Post date:** [July 17, 2008, 3:48am UTC](https://www.ruby-forum.com/t/method-missing-respond-to-and-ar/143304/2 "2008-07-17T03:48:14Z")

</div>

Sorry, I made a mistake.

Actually, Project has the attribute ‘mission’.

> > p.title\_before\_type\_cast  
> > =\> “Page title”
> 
> > p.mission  
> > =\> “Project mission”
> 
> > p.mission\_before\_type\_cast  
> > =\> SystemStackError…

So, p.respond\_to?(:mission\_before\_type\_cast) returns true AND  
p.send(:mission\_before\_type\_cast) that calls method\_missing at the same  
time?

I hope it is not so confusing.

Thanks

---

<div class="post-metadata">

**Author:** ![wawaloo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/bc8723/32.png) [@wawaloo](https://www.ruby-forum.com/u/wawaloo)\
**Post date:** [July 17, 2008, 9:58am UTC](https://www.ruby-forum.com/t/method-missing-respond-to-and-ar/143304/3 "2008-07-17T09:58:21Z")

</div>

On 17 Jul 2008, at 02:41, Wahid L. wrote:

> 

Because some activerecord methods are generated on the fly by method  
missing. respond\_to? returns true because if you were to call the  
method it wouldn’t raise NoMethodError or anything like that

Fred

---

<div class="post-metadata">

**Author:** ![wawaloo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/bc8723/32.png) [@wawaloo](https://www.ruby-forum.com/u/wawaloo)\
**Post date:** [July 17, 2008, 2:11pm UTC](https://www.ruby-forum.com/t/method-missing-respond-to-and-ar/143304/4 "2008-07-17T14:11:30Z")

</div>

Hi –

On Thu, 17 Jul 2008, Frederick C. wrote:

> > I don’t understand why respond\_to? returns true AND  
> > p.send(:title\_before\_type\_cast) calls method\_missing at the same time?
> 
> Because some activerecord methods are generated on the fly by method missing.  
> respond\_to? returns true because if you were to call the method it wouldn’t  
> raise NoMethodError or anything like that

respond\_to? actually also generates the methods on the fly.

> > t = Team.find(:first)  
> > =\> #\<Team id: 277401923, name: “Persuaders”, mascot: “Cat”,  
> > created\_at: “2008-07-16 00:23:09”, updated\_at: “2008-07-16 00:23:09”\>  
> > t.methods.grep(/mascot/)  
> > =\> []  
> > t.respond\_to?(:mascot)  
> > =\> true  
> > t.methods.grep(/mascot/)  
> > =\> [“mascot?”, “mascot”, “mascot=”]

Like method\_missing, it does this whether or not its argument is one  
of the column attributes. Using a new session and a new t:

> > t.methods.grep(/mascot/)  
> > =\> []  
> > t.respond\_to?(:blah)  
> > =\> false  
> > t.methods.grep(/mascot/)  
> > =\> [“mascot”, “mascot=”, “mascot?”]

In the default, out-of-the-box case, respond\_to? doesn’t necessarily  
predict whether NoMethodError will be raised:

class C  
def method\_missing(m)  
if /exists/.match(m.to\_s)  
puts “That method will be treated as existent!”  
end  
end  
end

c = C.new  
c.this\_sort\_of\_exists # “That method…”  
print "Do I respond to :this\_sort\_of\_exists?  
puts c.respond\_to?(:this\_sort\_of\_exists) # false

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:** ![wawaloo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/bc8723/32.png) [@wawaloo](https://www.ruby-forum.com/u/wawaloo)\
**Post date:** [July 17, 2008, 2:14pm UTC](https://www.ruby-forum.com/t/method-missing-respond-to-and-ar/143304/5 "2008-07-17T14:14:26Z")

</div>

On Thu, 17 Jul 2008, David A. Black wrote:

> print "Do I respond to :this\_sort\_of\_exists?

Whoops, add " at end of that line. (The code actually is  
tested; I deleted the " in the email in the course of adding comments  
🙂

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:** ![wawaloo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/bc8723/32.png) [@wawaloo](https://www.ruby-forum.com/u/wawaloo)\
**Post date:** [July 17, 2008, 5:08pm UTC](https://www.ruby-forum.com/t/method-missing-respond-to-and-ar/143304/6 "2008-07-17T17:08:54Z")

</div>

Thanks guys for the replies. It helped me understand some behaviours I  
found pretty strange.

I realized that my code had a contradiction that raised the  
SystemStackError.

class Project \< AR::Base  
def method\_missing(method\_name, \*args)  
if page.respond\_to?(method\_name)  
a = page.send(method\_name, \*args)  
elsif respond\_to?(method\_name)  
a = send(method\_name, \*args) #calls back method\_missing  
else  
super  
end  
return a  
end  
end

You can’t call a missing method inside method\_missing, and repond\_to? on  
that method doesn’t have sens since it is supposed to me missing in the  
first place.  
I also realized that some attributes were found due to some other code I  
had in a module.

New simplified method\_missing:

class Project \< AR::Base  
def method\_missing(method\_name, \*args)  
if page.respond\_to?(method\_name)  
a = page.send(method\_name, \*args)  
else  
super #looks for the method in AR::Base or raises NoMethodError  
end  
return a  
end  
end

Wahid
