Using belongs_to within instance method

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

On Mar 1, 8:06 pm, David [email protected] 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs