# Method\_missing in a AR child only catching 1 of several call

**URL:** <https://www.ruby-forum.com/t/method-missing-in-a-ar-child-only-catching-1-of-several-call/85198>\
**Category:** Rails\
**Created:** [February 11, 2007, 6:53am UTC](https://www.ruby-forum.com/t/method-missing-in-a-ar-child-only-catching-1-of-several-call/85198 "2007-02-11T06:53:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![acc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b38774/32.png) [@acc](https://www.ruby-forum.com/u/acc)\
**Post date:** [February 11, 2007, 6:53am UTC](https://www.ruby-forum.com/t/method-missing-in-a-ar-child-only-catching-1-of-several-call/85198/1 "2007-02-11T06:53:21Z")

</div>

Hello -

Why does a view including the following fragment:

```
 <% for column in User.content_columns %>
   <td><%=h user.send(column.name) %></td>
 <% end %>

```

and the User model having the following defined:

```
 def method_missing(meth_id, *args, &blk)
   meth_name=meth_id.id2name
   puts("Calling: #{meth_name}")
   super
 end

```

only result in the following output of one method call when there are  
several columns/attributes in the users table:

```
 Calling: name
```

---

<div class="post-metadata">

**Author:** ![acc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b38774/32.png) [@acc](https://www.ruby-forum.com/u/acc)\
**Post date:** [February 11, 2007, 9:35am UTC](https://www.ruby-forum.com/t/method-missing-in-a-ar-child-only-catching-1-of-several-call/85198/2 "2007-02-11T09:35:43Z")

</div>

On 2/11/07, Aakash C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> def method_missing(meth_id, *args, &blk)
> meth_name=meth_id.id2name
> puts("Calling: #{meth_name}")
> super
> end
> 
> ```
> 
> only result in the following output of one method call when there are  
> several columns/attributes in the users table:
> 
> ```
> Calling: name
> 
> ```

Hi Aakash,

ActiveRecord::Base defines read methods lazily. That is, until you  
call a method to read an attribute, the method won’t exist. Call a  
method to read an attribute, and it’ll hit AR::B’s method\_missing  
hook, which defines all the read methods, and hence, subsequent calls  
to read methods won’t trigger method\_missing. You can witness the  
effect in script/console:

> > Thing.instance\_methods(false)  
> > =\> []  
> > Thing.new.name  
> > =\> nil  
> > Thing.instance\_methods(false)  
> > =\> [“id?”, “name?”, “name”, “value”, “value?”]

I’m not sure what you’re trying to do, but if you explain it, someone  
might be able to find something that works. 🙂 (Assuming this is a  
problem for you.)

Regards,  
George.

---

<div class="post-metadata">

**Author:** ![acc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b38774/32.png) [@acc](https://www.ruby-forum.com/u/acc)\
**Post date:** [February 11, 2007, 9:56am UTC](https://www.ruby-forum.com/t/method-missing-in-a-ar-child-only-catching-1-of-several-call/85198/3 "2007-02-11T09:56:05Z")

</div>

George O. wrote:  
.  
.

> I’m not sure what you’re trying to do, but if you explain it, someone  
> might be able to find something that works. 🙂 (Assuming this is a  
> problem for you.)
> 
> Regards,  
> George.

Hey George - Thanks for the quick response.

I’d like intercept all method calls to my models accessing an attribute  
in order to implement an attribute level authorization scheme - similar  
to the J2EE analog which is a pain but works (specified in ejb-jar.xml).

I think I found a way to configure rails to not generate the ENTIRE  
method list lazily - but the implication is that I’ll loose on  
performance:

config.active\_record.generate\_read\_methods = false (instead of the  
default value of true)

Anyone got any Ideas?

Regards,  
Aakash

---

<div class="post-metadata">

**Author:** ![acc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b38774/32.png) [@acc](https://www.ruby-forum.com/u/acc)\
**Post date:** [February 11, 2007, 10:46am UTC](https://www.ruby-forum.com/t/method-missing-in-a-ar-child-only-catching-1-of-several-call/85198/4 "2007-02-11T10:46:51Z")

</div>

On 2/11/07, AC CE [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> config.active\_record.generate\_read\_methods = false (instead of the  
> default value of true)
> 
> Anyone got any Ideas?

Have you looked around at the existing model-level authentication  
plugins? I think some do attribute-level authorization. e.g., [1].

If that doesn’t fly for you, I’m not sure what the best way to do this  
is. Once you’ve generated the read methods, they’re available via  
ModelName.read\_methods. Perhaps you can override these. I don’t  
think that’s particularly watertight, though. #read\_attribute, for  
example seems to pull things directly out of the @attributes hash  
inside the active record instances directly rather than going through  
the read methods. Perhaps you could hook into the #[] method on this  
hash, but that’s getting pretty nasty!

class ActiveRecord::Base  
def initialize\_with\_authorization\_check(\*args)  
initialize\_without\_authorization\_check(\*args) do |\*args|  
class \<\< @attributes  
def   
# … authorization check …  
super  
end  
def []=(name, value)  
# … authorization check …  
super  
end  
end  
yield(\*args) if block\_given?  
end  
end  
alias initialize\_without\_authorization\_check initialize  
alias initialize initialize\_with\_authorization\_check  
end

(Totally untested.)

[1] [http://perens.com/FreeSoftware/ModelSecurity/](http://perens.com/FreeSoftware/ModelSecurity/)

George.
