57

So I know how to override the default getters for attributes of an ActiveRecord object using

def custom_getter
  return self[:custom_getter] || some_default_value
end

I'm trying to achieve the same thing however for a belongs to association. For instance.

class Foo < AR
  belongs_to :bar

  def bar
    return self[:bar] || Bar.last
  end
end

class Bar < AR
  has_one :foo
end

When I say:

f = Foo.last

I'd like to have the method f.bar return the last Bar, rather than nil if that association doesn't exist yet.

This doesn't work however. The reason is that self[:bar] is always undefined. It's actually self[:bar_id].

I can do something naive like:

def bar
  if self[:bar_id]
    return Bar.find(self[:bar_id])
  else
    return Bar.last
  end
end

However this will always make a db call, even if Bar has already been fetched, which is certainly not ideal.

Does anyone have an insight as to how I might have a relationship such that the belongs_to attribute is only loaded once and has a default value if not set.

4 Answers 4

78

alias_method is your friend here.

alias_method :original_bar, :bar
def bar
  self.original_bar || Bar.last
end

The way this works is that you alias the default "bar" method as "original bar" and then implement your own version of "bar". If the call to original_bar returns nil then you return the last Bar instance instead.

7
  • You don't have a loop with that ? because call bar in bar ?
    – shingara
    Mar 23, 2010 at 14:50
  • No, you call "original_bar" which is the new name of "bar". Mar 23, 2010 at 15:08
  • 1
    One thing that tripped me up, was to remember to put the alias_method call BEFORE the new aliased method
    – Hortitude
    Feb 19, 2012 at 22:09
  • 5
    couldn't line 3 be just original_bar || Bar.last
    – user160917
    Mar 18, 2013 at 21:59
  • 4
    Better: super || Bar.last
    – Brian
    Jun 24, 2015 at 22:20
39

i found that using "super" is the best way

def bar
  super || Bar.last
end

I hope this helps you :D

1
  • 2
    This is such a great answer, as it just uses plain old Ruby inheritance. And it's a lot simpler than the accepted answer. Apr 5, 2019 at 16:43
10

Randy's answer is spot on, but there's an easier way to write it, using alias_method_chain:


def bar_with_default_find
  self.bar_without_default_find || Bar.last
end
alias_method_chain :bar, :default_find

That creates two methods - bar_with_default_find and bar_without_default_find and aliases bar to the with method. That way you can explicitly call one or the other, or just leave the defaults as is.

2
  • Cool solution because you can use _without_default_find to have a notion of the original assocation method. Was the only one I was able to get working. Thanks
    – grant
    Jun 2, 2016 at 18:15
  • I believe alias_method_chain is deprecated now.
    – Nick
    Mar 3, 2020 at 14:14
0

Building upon other answers here, you may also want to handle assignment operations as well.

Alias method:

alias_method :original_bar, :bar
alias_method :original_bar=, :bar=
def bar
  self.original_bar ||= Bar.last
end

Super method:

def bar
  super || bar = Bar.last
end

Where this was useful for me was when using Bar.find_or_initialize_by which meant the record wasn't always persisted, and also any non-persisted changes would reflect on the parent record as well.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.