This project is archived and is in readonly mode.
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
-
Jeremy Kemper June 21st, 2010 @ 01:43 AM
- Milestone set to 2.3.9
- State changed from new to open
-
Tor Erik July 2nd, 2010 @ 04:05 PM
- Importance changed from to Low
Note that it works as expected
car.drivers.first
is changed to
car.drivers.to_a.first
-
Santiago Pastorino February 2nd, 2011 @ 04:49 PM
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.
-
Santiago Pastorino February 2nd, 2011 @ 04:49 PM
- State changed from open to stale
-
holli February 6th, 2011 @ 12:59 AM
- State changed from stale to open
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]
-
Santiago Pastorino February 6th, 2011 @ 09:17 PM
- Milestone changed from 2.3.10 to 3.1
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Referenced by
- 6684 [TEST] Inverse of doesn't work when associations eager load other associations Never mind. This is a duplicate of #4914. [state:duplicate]