Skip to content

ActiveRecord :inverse_of doesn't have any effect #689

Closed
lighthouse-import opened this Issue May 16, 2011 · 6 comments

2 participants

@lighthouse-import

Imported from Lighthouse. Original ticket at: http://rails.lighthouseapp.com/projects/8994/tickets/4914
Created by Hongli Lai - 2011-03-30 04:35:48 UTC

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.

@lighthouse-import

Imported from Lighthouse.
Comment by Tor Erik - 2010-07-02 15:05:57 UTC

Note that it works as expected

car.drivers.first

is changed to

car.drivers.to_a.first
@lighthouse-import

Imported from Lighthouse.
Comment by Tor Erik - 2010-07-02 15:07:21 UTC

(... it works as expected IF)

@lighthouse-import

Imported from Lighthouse.
Comment by Jeremy Kemper - 2010-08-30 01:28:51 UTC

[bulk edit]

@lighthouse-import

Imported from Lighthouse.
Comment by Santiago Pastorino - 2011-02-02 16:49:40 UTC

This issue has been automatically marked as stale because it has not been commented on for at least three months.

The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

@lighthouse-import

Imported from Lighthouse.
Comment by holli - 2011-02-06 00:59:42 UTC

Still open. Noticed it in my code using rails 3.0.3. I did also try it with using the code from above ( http://gist.github.com/445969 ).

Seems like fetching all with .to_a without any other options is the only way to get expected results.

Car.find(1).drivers.first.car
# Car Load (0.2ms)  SELECT "cars".* FROM "cars" WHERE ("cars"."id" = 1) LIMIT 1
# Driver Load (0.2ms)  SELECT "drivers".* FROM "drivers" WHERE ("drivers".car_id = 1) LIMIT 1
# Car Load (0.1ms)  SELECT "cars".* FROM "cars" WHERE ("cars"."id" = 1) LIMIT 1

# This works as excepted, no new query =>
Car.find(1).drivers.to_a.first.car
# Car Load (0.4ms)  SELECT "cars".* FROM "cars" WHERE ("cars"."id" = 1) LIMIT 1
# Driver Load (0.3ms)  SELECT "drivers".* FROM "drivers" WHERE ("drivers".car_id = 1)

Car.find(1).drivers.order('id').to_a.first.car
# Car Load (0.1ms)  SELECT "cars".* FROM "cars" WHERE ("cars"."id" = 1) LIMIT 1
# Driver Load (0.2ms)  SELECT "drivers".* FROM "drivers" WHERE ("drivers".car_id = 1) ORDER BY id
# Car Load (0.2ms)

[state:open]

@jake3030 jake3030 pushed a commit to jake3030/rails that referenced this issue Jun 28, 2011
@dhh dhh Make #destroy write 1 line into log (instead of 3) (Dmitry Sokurenko) [
…#689 status:committed]
8f0f078
@gamov
gamov commented Jun 6, 2012

This is still the case for me, just tested it with 3.2.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.