Skip to content

Instantly share code, notes, and snippets.

@pat
Created February 13, 2009 03:57
Show Gist options
  • Save pat/63040 to your computer and use it in GitHub Desktop.
Save pat/63040 to your computer and use it in GitHub Desktop.
# Goal is => Client.first.items... Can do with finder_sql, but want an activerecord way!
class Client < ActiveRecord::Base
has_many :contacts
has_many :tasks, :through => :contacts
# has_many :items, :through => :tasks # THIS DOESNT WORK
def items
@items ||= self.tasks.collect { |task| task.items }.flatten
end
end
class Contact < ActiveRecord::Base
belongs_to :client
has_many :tasks
end
class Task < ActiveRecord::Base
belongs_to :contact
has_many :items
end
class Item < ActiveRecord::Base
belongs_to :task
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment