Trying to override assocation find

Hey Guys,
I’m stumped with something, hoping somebody can help.

Basically, I’m trying to override the default belongs_to accessor.

For example, I have an Event model, that belongs_to a Venue:

class Event < ActiveRecord::Base
belongs_to :venue


end

The venue is not always set, so sometimes “event.venue” returns null.
I want to be able to override “event.venue”, and provide my own code
that grabs the venue if it doesn’t exist. I was thinking I could do
something like:

def venue
if self.send(:venue) == nil
# code to fetch the venue and insert into db
else
self.send(:venue)
end
end

Clearly though, this doesn’t work, because self.send(:venue) just calls
itself, and ruby barfs on a stack overflow from the recursion.

The workaround is to create a “get_venue” method, and always call that
instead of “venue,” but that seems kind of icky.

Any ideas?

Spencer

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