# Acquire parent attribute from child via belongs\_to?

**URL:** <https://www.ruby-forum.com/t/acquire-parent-attribute-from-child-via-belongs-to/140198>\
**Category:** Rails\
**Created:** [June 11, 2008, 8:47pm UTC](https://www.ruby-forum.com/t/acquire-parent-attribute-from-child-via-belongs-to/140198 "2008-06-11T20:47:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [June 11, 2008, 8:47pm UTC](https://www.ruby-forum.com/t/acquire-parent-attribute-from-child-via-belongs-to/140198/1 "2008-06-11T20:47:56Z")

</div>

Poking around books & blogs about association proxies, and  
experimenting, but I can’t seem to come across the right combination  
for this one particular task.

I’m assuming there’s a way to get the “user\_type” attribute from  
within the UserFilter using the belongs\_to association?

class PrivilegedUser \< ActiveRecord::Base  
has\_many :user\_filters

# has an attribute named user\_type

end

class UserFilter \< ActiveRecord::Base

belongs\_to :privileged\_user

def self.whatever  
x = self.privileged\_user.user\_type \<------ ???  
end

end

The example above gives me undefined method errors.

– gw

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [June 11, 2008, 10:18pm UTC](https://www.ruby-forum.com/t/acquire-parent-attribute-from-child-via-belongs-to/140198/2 "2008-06-11T22:18:41Z")

</div>

On 11 Jun 2008, at 19:47, Greg W. wrote:

> has\_many :user\_filters
> 
> # has an attribute named user\_type
> 
> end
> 
> class UserFilter \< ActiveRecord::Base
> 
> belongs\_to :privileged\_user
> 
> def self.whatever  
> x = self.privileged\_user.user\_type \<------ ???  
> end

That can’t work - your whatever method is a class method, so  
self.privileged user is meaningless (since self is the class here). If  
whatever was an instance methood it should work as you’ve written it

Fred
