This project is archived and is in readonly mode.

#833 ✓wontfix
Orion Delwaterman

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

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>

Pages