Nested has_many :through

Hi (Oh my God… he just started his blog post with ‘Hi’),

Did you know that as of Rails 3.1, you can have nested has_many :through clauses in your models? Here is a concrete example to understand what I mean:

[ruby]
class SportSchedule < ActiveRecord::Base
has_many :gamedays
has_many :games, :through => :gamedays
has_many :results, :through => :games # <= You could not do this before Rails 3.1


def has_results?
!self.results.empty?
end
end
[/ruby]

  • benjamin

    !self.results.empty? => results.any?

  • http://www.rubyfleebie.com Frank

    Thanks Benjamin! Didn’t know about the any? method

  • Steven Soroka

    This seems like a work-around for arel not supporting schedule.gamedays.games.results