Created
June 20, 2010 17:44
-
-
Save FooBarWidget/445969 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>> 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I still have this problem with rails 3.2.5