Skip to content

Instantly share code, notes, and snippets.

@FooBarWidget
Created Jun 20, 2010
Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@gamov 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