Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #3358 (closed defect: duplicate)

Opened 4 years ago

Last modified 4 years ago

STI can break eager loading

Reported by: bodhi@theplant.jp Assigned to: David
Priority: normal Milestone: 1.1
Component: ActiveRecord Version: 1.0.0
Severity: minor Keywords: sti eager loading
Cc: jonathan@bluewire.net.nz

Description

with the following setup

class User < ActiveRecord::Base
   has_one :image, :class_name => "UserImage", :foreign_key => "parent_id"
end

class UserImage < Image
   belongs_to :user, :foreign_key => "parent_id"
end

class Image < ActiveRecord::Base
end

doing a

User.find(:all, :include => :image)

wont return users with null images,

because ActiveRecord generates a query that appends

WHERE (images.`type` = 'UserImage' ) 

to the query. This eliminates all the users that have no image as on the join result they have a null image type.

Change History

12/31/05 09:00:21 changed by mjb@asplake.co.uk

I added a comment about this to the Rails wiki yesterday. See http://wiki.rubyonrails.com/rails/pages/WhenToUseTheModelMethod and http://www.livejournal.com/users/asplake/715.html.

Personally I think this is a feature rather than a bug, but I suppose it is slightly weird that a superclass should be dependent on its subclasses. Easily worked round anyway.

12/31/05 12:39:04 changed by anonymous

Simple unit tests written up here: http://www.livejournal.com/users/asplake/942.html

Not sure whether (and where) to add this to the wiki - someone drop me a line if you think I should. I'm new to all this...

03/08/06 03:33:26 changed by Jonathan

  • status changed from new to closed.
  • resolution set to duplicate.
  • milestone set to 1.1.

This was fixed in r3776. The eager loading should work as expected now.

03/08/06 03:45:40 changed by anonymous

  • cc set to jonathan@bluewire.net.nz.