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.

AR belongs_to :include option doesn't do anything

#3455

It seems that eager loading of AR for belongs_to associations is broken.

With the new strategy for has_many associations, belongs_to doesn't left join relevant tables (which is only logical thing to do for 1:1 relationships).

So

class SolarSystem < ActiveRecord::Base
  has_many :fow_ss_entries, :dependent => :delete_all

class FowSsEntry < ActiveRecord::Base
  belongs_to :galaxy
  belongs_to :solar_system
  belongs_to :player

  named_scope :for, Proc.new { |galaxy, player|
    galaxy = galaxy.id if galaxy.is_a? Galaxy
    player = player.id if player.is_a? Player

    {
      :conditions => {:galaxy_id => galaxy, :player_id => player},
      :include => :solar_system
    }
  }
FowSsEntry.for(1,1).each { |fse| fse.solar_system }

Generates this:

[2009-11-03 00:35:26|main|debug] FowSsEntry Load (0.0ms)  SELECT * FROM 
`fow_ss_entries` WHERE (`fow_ss_entries`.`galaxy_id` = 1 AND 
`fow_ss_entries`.`player_id` = 1)
[2009-11-03 00:35:26|main|debug] SolarSystem Load (0.0ms)  SELECT * FROM 
`solar_systems` WHERE (`solar_systems`.`id` = 57)
[2009-11-03 00:35:26|main|debug] SolarSystem Load (0.0ms)  SELECT * FROM 
`solar_systems` WHERE (`solar_systems`.`id` = 157)
[2009-11-03 00:35:26|main|debug] SolarSystem Load (0.0ms)  SELECT * FROM 
`solar_systems` WHERE (`solar_systems`.`id` = 313)
[2009-11-03 00:35:26|main|debug] SolarSystem Load (0.0ms)  SELECT * FROM 
`solar_systems` WHERE (`solar_systems`.`id` = 314)

It should be left outer join. If you force it into join mode, by providing :order => "solar_systems.id" to the scope, it works as expected, but it shouldn't be that way.

Reported by Artūras Šlajus · November 3rd, 2009 @ 10:50 AM

State: stale
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. David Trasbo
    David Trasbo

    I was able to duplicate this with version 2.3.5 of Rails:

    ➜  code  rails -v                        
    Rails 2.3.5
    ➜  code  rails named_scope_include_option
    ...
    ➜  code  cd named_scope_include_option 
    ➜  named_scope_include_option  script/generate model foo bar_id:integer
    ...
    ➜  named_scope_include_option  script/generate model bar
    ...
    ➜  named_scope_include_option  rake db:migrate
    (in /Users/dtrasbo/code/named_scope_include_option)
    ...
    ➜  named_scope_include_option  script/console
    Loading development environment (Rails 2.3.5)
    ruby-1.8.7-p249 > Foo.baz.each do |foo|
    ruby-1.8.7-p249 >     foo.bar
    ruby-1.8.7-p249 ?>  end
     => [#<Foo id: 1, bar_id: 1, created_at: "2010-04-14 19:31:29", updated_at: "2010-04-14 19:31:29">]
    

    Sure enough, the log shows two entries:

    SELECT * FROM "foos"
    SELECT * FROM "bars" WHERE ("bars"."id" = 1)
    

    April 14th, 2010 @ 08:42 PM

  2. David Trasbo
    David Trasbo

    Here's the Foo model, by the way. Bar has no code in it.

    class Foo < ActiveRecord::Base
      belongs_to :bar
      named_scope :baz, :include => :bar
    end
    

    April 14th, 2010 @ 08:43 PM

  3. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open
    • Tag changed from 2.3.4, activerecord to 234, activerecord

    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:35 PM

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

    February 2nd, 2011 @ 04:35 PM

  5. bingbing
  6. Julian Mehnle
    Julian Mehnle
    • Tag changed from 234, activerecord to 2.3.11, 2311, 234, activerecord

    This is still broken in 2.3.11. Didn't have a chance to test it with ActiveRecord/Rails 3 yet.

    March 29th, 2011 @ 06:37 AM

  7. Dave Schweisguth
    Dave Schweisguth

    In Rails 3.0.6

    • when querying multiple objects, eager-loaded belongs_to are loaded in a single separate query with 'where id in (...)', which makes sense since left joining could bring in the same date more than once. I'd have sworn this was the Rails 2.3.11 behavior but I no longer have a Rails 2.3.11 app handy to check.

    • there is still a bug in that when querying a single object with find, ActiveRecord should know that it can left join, but it still takes a second query to get the belonged-to object.

    April 10th, 2011 @ 01:27 AM