# Get the object caller

**URL:** <https://www.ruby-forum.com/t/get-the-object-caller/182990>\
**Category:** Rails\
**Created:** [February 5, 2010, 6:16pm UTC](https://www.ruby-forum.com/t/get-the-object-caller/182990 "2010-02-05T18:16:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Augusto\_Ruibal\_SShi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@Augusto\_Ruibal\_SShi](https://www.ruby-forum.com/u/Augusto_Ruibal_SShi)\
**Post date:** [February 5, 2010, 6:16pm UTC](https://www.ruby-forum.com/t/get-the-object-caller/182990/1 "2010-02-05T18:16:08Z")

</div>

Let me explain

I have these models

class User \< AR::Base  
has\_many :contact  
end

class Contact \< AR::Base  
belongs\_to :user  
belongs\_to :user\_contact\_id, :class\_name =\> “User”, :foreign\_key =\>  
“user\_contact\_id” # I have field called :user\_contact\_id who is the  
another user record of the user table.

def self.is\_contact?(user\_contact\_id)  
# CHECK IF THE USER ALREADY HAVE A CONTACT WITH THE USER\_CONTACT\_ID

# 

end  
end

I’ve an instantiated User class, @user, and want to do the following

@user.contacts.is\_contact?(user\_contact\_id) # This works like a charm.

My question is, how can i access the @user caller object from the  
self.is\_contact? method in Contact Class? Is this possible?

Thanks.

---

<div class="post-metadata">

**Author:** ![Augusto\_Ruibal\_SShi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@Augusto\_Ruibal\_SShi](https://www.ruby-forum.com/u/Augusto_Ruibal_SShi)\
**Post date:** [February 5, 2010, 7:05pm UTC](https://www.ruby-forum.com/t/get-the-object-caller/182990/2 "2010-02-05T19:05:33Z")

</div>

> @user.contacts.is\_contact?(user\_contact\_id) # This works like a charm  
> It does? What’s the content of the “is\_contract?” function?

It might be easier to help if you tell us what you’re trying to achieve.  
What are you going to do once you know if the user have / doesn’t have a  
contract with that user\_contract\_id?

---

<div class="post-metadata">

**Author:** ![Augusto\_Ruibal\_SShi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@Augusto\_Ruibal\_SShi](https://www.ruby-forum.com/u/Augusto_Ruibal_SShi)\
**Post date:** [February 6, 2010, 11:35am UTC](https://www.ruby-forum.com/t/get-the-object-caller/182990/3 "2010-02-06T11:35:32Z")

</div>

On Feb 5, 3:22 pm, “Augusto Ruibal (Shinta)”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
e an instantiated User class, @user, and want to do the following

> @user.contacts.is\_contact?(user\_contact\_id) # This works like a charm.
> 
> My question is, how can i access the @user caller object from the  
> self.is\_contact? method in Contact Class? Is this possible?

you can’t. What do you need it for ? - any find you do on Contacts  
from inside is\_contact? will be magically scoped to @user’s contacts  
anyway.

Fred

---

<div class="post-metadata">

**Author:** ![Augusto\_Ruibal\_SShi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@Augusto\_Ruibal\_SShi](https://www.ruby-forum.com/u/Augusto_Ruibal_SShi)\
**Post date:** [February 6, 2010, 8:26pm UTC](https://www.ruby-forum.com/t/get-the-object-caller/182990/4 "2010-02-06T20:26:59Z")

</div>

On Feb 5, 10:22 am, “Augusto Ruibal (Shinta)”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> belongs_to :user_contact_id, :class_name => "User", :foreign_key =>
> 
> ```
> 
> @user.contacts.is\_contact?(user\_contact\_id) # This works like a charm.

A plain :through could do what you’re looking for:

class Contact \< AR::Base  
belongs\_to :user  
belongs\_to :user\_contact, :class\_name =\> “User”  
end

class User \< AR::Base  
has\_many :contacts  
has\_many :user\_contacts, :through =\> :contacts  
end

(you may need a :source and/or :class\_name on :user\_contacts, try it  
and see)

Then you can do:

@user.user\_contacts.include?(some\_other\_user)

–Matt J.
