Skip to content

Instantly share code, notes, and snippets.

@adamcooper
Created December 1, 2008 01:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamcooper/30580 to your computer and use it in GitHub Desktop.
Save adamcooper/30580 to your computer and use it in GitHub Desktop.
class User < ActiveRecord::Base
has_many :cars
end
class Car < ActiveRecord::Base
belongs_to :user, :counter_cache => true
end
user = User.create
=> #<User id: 1, cars_count: 0, created_at: "2008-01-14 20:40:14", updated_at: "2008-01-14 20:41:52">
car = Car.create
=> #<Car id: 1, user_id: nil, created_at: "2008-01-14 20:40:24", updated_at: "2008-01-14 20:41:50">
>> user.cars.size
=> 0
>> car.user = user
=> #<User id: 1, cars_count: 0, created_at: "2008-01-14 20:40:14", updated_at: "2008-01-14 20:41:52">
>> user
=> #<User id: 1, cars_count: 0, created_at: "2008-01-14 20:40:14", updated_at: "2008-01-14 20:41:52">
# First issue: users cars_count does not get updated in memory.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment