7

Let's say that I'm retrieving the name of the sport by calling the following chain of associations:

pick.event.league.sport.name

How can I use the delegate method so that I can just call *pick.event_league_sport_name* like so? Obviously, I can create a method in the pick model:

def event_league_sport_name
     return self.event.league.sport.name
end

But I want to use the delegate method!!!

keruilin
  • 16,782
  • 34
  • 108
  • 175

2 Answers2

14

I dont suggest this , but if you want ...

delegate :name , :to => "event.league.sport" ,:prefix=>"event_league_sport"

also without prefix.

be sure to handle nil associations ...

have a nice day!

andrea
  • 3,515
  • 2
  • 22
  • 14
5

You could do the following:

class Pick
  def sport
    event.league.sport
  end

  delegate :name, :to => :sport
end

This would result in pick.name being equivalent to pick.event.league.sport.name.

Pan Thomakos
  • 34,082
  • 9
  • 88
  • 85