Skip to content

Instantly share code, notes, and snippets.

@workmaster2n
Created January 15, 2014 19:41
Show Gist options
  • Save workmaster2n/8443003 to your computer and use it in GitHub Desktop.
Save workmaster2n/8443003 to your computer and use it in GitHub Desktop.
Cacheing has_many through:
p = Person.create
p.items << Item.create
Rails.cache.write(Time.now, p)
=> false
p.reload
Rails.cache.write(Time.now, p)
=> <truthy>
class Item < ActiveRecord::Base
attr_accessible :description
has_many :person_items
has_many :people, through: :person_items
end
class Person < ActiveRecord::Base
attr_accessible :name
has_many :person_locations
has_many :locations, through: :person_locations
has_many :person_items
has_many :items, through: :person_items
end
class PersonItem < ActiveRecord::Base
attr_accessible :item_id, :person_id
belongs_to :item
belongs_to :person
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment