This project is archived and is in readonly mode.
Eager Loading does not work for Polymorphic Relationships
Reported by Orion Delwaterman | August 14th, 2008 @ 06:58 PM | in 2.x
The ActiveRecord class method "find"'s include option does not work with polymorphic relationships:
migratation
class CreateSampleMigration < ActiveRecord::Migration def self.up
create_table :content_items do |t|
t.string :name, :null => false
t.timestamps
end
create_table :system_blobs do |t|
t.integer :blobable_id, :null => false
t.string :blobable_type, :null => false, :limit => 100
t.binary :data, :limit => 2.megabytes
t.timestamps
end
end end
ContentItem model
Class ContentItem < ActiveRecord::Base has_one :system_blob, :as => "blobable",
:dependent => :delete
end
SystemBlob model
class SystemBlob < ActiveRecord::Base belongs_to :blobable, :polymorphic => true end
Test code
c = ContentItem.new(:name => "test") c.save! c.create_system_blob(:data => "TEST_DATA")
found_c = ContentItem.find(:first, :conditions => {:name => "test"}, :include => :system_blob found_c.system_blob
=> nil
found_c.system_blob(true)
=> <#SystemBlob ...
Comments and changes to this ticket
-
Frederick Cheung August 21st, 2008 @ 09:06 PM
- Tag changed from layer idsearchwp-highlighting9 highlightterm-3 classsearchwp-termeagerlayer_layer idsearchwp-highlighting10 highlightterm-4 classsearchwp-termloadinglayer, activerecord to activerecord, eager_loading, ploymorphic
Do the queries made look sane ?
-
Frederick Cheung August 21st, 2008 @ 09:12 PM
Oops, not sure what happened with the tags there! I bashed in your code against edge and 2.1 and couldn't reproduce it
-
josh November 22nd, 2008 @ 06:49 PM
- State changed from new to wontfix
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>