Forum: Ruby on Rails Using belongs_to within instance method

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
David (Guest)
on 2009-03-01 22:07
(Received via mailing list)
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)
#this works but im wondering if its possible to do something like:

id =

Frederick C. (Guest)
on 2009-03-01 22:42
(Received via mailing list)
On Mar 1, 8:06 pm, David <removed_email_address@domain.invalid> wrote:
> #this works but im wondering if its possible to do something like:
> id =
Unless I'm horribly misunderstanding, why not do  ?
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

is the same as [1]


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

This topic is locked and can not be replied to.