Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

ActiveRecord :inverse_of doesn't have any effect

#4914

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.

Reported by Hongli Lai · June 20th, 2010 @ 06:58 PM

State: open
Milestone: 3.1
Assigned to: nobody
Importance: Low

Activity

  1. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 2.3.9
    • State changed from new to open

    June 21st, 2010 @ 01:43 AM

  2. Tor Erik
    Tor Erik
    • Importance changed from to Low

    Note that it works as expected

    car.drivers.first
    

    is changed to

    car.drivers.to_a.first
    

    July 2nd, 2010 @ 04:05 PM

  3. Tor Erik
    Tor Erik

    (... it works as expected IF)

    July 2nd, 2010 @ 04:07 PM

  4. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.9 to 2.3.10

    August 30th, 2010 @ 02:28 AM

  5. Santiago Pastorino
    Santiago Pastorino

    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.

    February 2nd, 2011 @ 04:49 PM

  6. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:49 PM

  7. holli
    holli
    • 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]

    February 6th, 2011 @ 12:59 AM

  8. Santiago Pastorino
    Santiago Pastorino
    • Milestone changed from 2.3.10 to 3.1

    February 6th, 2011 @ 09:17 PM