# Using belongs\_to within instance method

**URL:** <https://www.ruby-forum.com/t/using-belongs-to-within-instance-method/161729>\
**Category:** Rails\
**Created:** [March 1, 2009, 9:07pm UTC](https://www.ruby-forum.com/t/using-belongs-to-within-instance-method/161729 "2009-03-01T21:07:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [March 1, 2009, 9:07pm UTC](https://www.ruby-forum.com/t/using-belongs-to-within-instance-method/161729/1 "2009-03-01T21:07:29Z")

</div>

Is it possible to use model associations with an instance method? For  
example, I have:

Class Reminder  
belongs\_to :event

#and i want to access the event id within a Reminder instance method:  
def find\_event\_id

id = Event.find(event\_id).user.id  
#this works but im wondering if its possible to do something like:

id = this\_reminder.event.user.id?

end

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [March 1, 2009, 9:42pm UTC](https://www.ruby-forum.com/t/using-belongs-to-within-instance-method/161729/2 "2009-03-01T21:42:13Z")

</div>

On Mar 1, 8:06 pm, David [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> #this works but im wondering if its possible to do something like:
> 
> id = this\_reminder.event.user.id?

Unless I’m horribly misunderstanding, why not do event.user.id ?  
When you do belongs\_to :event that creates an instance method called  
event on the Reminder class, so you can just go ahead and call this.  
The ‘this\_reminder’ you wrote is basically self but if you don’t  
specify a target for your method calls the ruby assumes self ie

self.event.user.id

is the same as

event.user.id [1]

Fred

[1] Not quite the same. If you had a local variable called event then  
when you wrote event.user.id ruby would assume that you were talking  
about the local variable not the method whereas with self there is no  
ambiguity

Fred
