Let's say that I get the name of the sport, causing 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 * in the same way? Obviously, I can create a method in the selection model:
def event_league_sport_name return self.event.league.sport.name end
But I want to use the delegate method .
You can do the following:
class Pick def sport event.league.sport end delegate :name, :to => :sport end
This will cause pick.name be equivalent to pick.event.league.sport.name .
pick.name
I do not suggest this, but if you want ...
delegate :name , :to => "event.league.sport" ,:prefix=>"event_league_sport"
also without a prefix.
be sure to handle nil associations ...
we have a good day!
Source: https://habr.com/ru/post/1343086/More articles:XML Collections SChema - xmlGoal C: Release int / float properties - memory-managementHow to install the program only for local user in Linux? - linuxUsing WP_List_Table in the plugin - oopIs it possible to generate a list of all marked lines in C # at compile time / runtime? - stringNSManagedObject initWithEntity: insertIntoManagedObjectContext: EXC_BAD_ACCESS - iosHow can I get the highest number in this Linq result? - c #Is it possible to create a global list of marked lines at compile time / runtime? - c ++https://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1343090/convert-list-of-dicts-to-string&usg=ALkJrhhuYxEN1iP7k3wHOaxj_Ny23T0ZjwORA-01795: the maximum number of expressions in the list is 1000 - stringbuilderAll Articles