This project is archived and is in readonly mode.

#4914 open
Hongli Lai

ActiveRecord :inverse_of doesn't have any effect

Reported by Hongli Lai | June 20th, 2010 @ 06:58 PM | in 3.1

Suppose that a Car has_many Drivers, and that the association methods on both ends have :inverse_of specified. Code here: http://gist.github.com/445969
One would expect that if one references a driver through a car, and then follows the association back to car, no new SQL query would be performed if :inverse_of is set. But a query is performed.

Expected behavior:

car = Car.find(1)
# Car Load (0.3ms)   SELECT * FROM "cars" WHERE ("cars"."id" = 1) 

driver = car.drivers.first
# Driver Load (0.2ms)   SELECT * FROM "drivers" WHERE ("drivers".car_id = 1) LIMIT 1

driver.car
# No SQL query here.

Actual behavior:

car = Car.find(1)
# Car Load (0.3ms)   SELECT * FROM "cars" WHERE ("cars"."id" = 1) 

driver = car.drivers.first
# Driver Load (0.2ms)   SELECT * FROM "drivers" WHERE ("drivers".car_id = 1) LIMIT 1

driver.car
# Car Load (0.2ms)   SELECT * FROM "cars" WHERE ("cars"."id" = 1)

This happens on both Rails 2.3.8 and Rails 3 git master.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Tickets have moved to Github

The new ticket tracker is available at https://github.com/rails/rails/issues

Shared Ticket Bins

Referenced by

Pages