Skip to content

Instantly share code, notes, and snippets.

@FooBarWidget
Created June 20, 2010 17:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FooBarWidget/445969 to your computer and use it in GitHub Desktop.
Save FooBarWidget/445969 to your computer and use it in GitHub Desktop.
class Car < ActiveRecord::Base
has_many :drivers, :inverse_of => :car
end
class CreateCars < ActiveRecord::Migration
def self.up
create_table :cars do |t|
end
end
def self.down
drop_table :cars
end
end
class Driver < ActiveRecord::Base
belongs_to :car, :inverse_of => :drivers
end
class CreateDrivers < ActiveRecord::Migration
def self.up
create_table :drivers do |t|
t.references :car
end
end
def self.down
drop_table :drivers
end
end
>> c = Car.create
=> #<Car id: 1, created_at: "2010-06-20 17:39:26", updated_at: "2010-06-20 17:39:26">
>> c.drivers.create
=> #<Driver id: 1, car_id: 1>
>> Car.find(1).drivers.first.car
Car Load (0.3ms) SELECT * FROM "cars" WHERE ("cars"."id" = 1)
Driver Load (0.2ms) SELECT * FROM "drivers" WHERE ("drivers".car_id = 1) LIMIT 1
Car Load (0.2ms) SELECT * FROM "cars" WHERE ("cars"."id" = 1)
@gamov
Copy link

gamov commented Jun 6, 2012

I still have this problem with rails 3.2.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment