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.

Eager Loading does not work for Polymorphic Relationships

#833

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 ...

Reported by Orion Delwaterman · August 14th, 2008 @ 06:58 PM

State: wontfix
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Frederick Cheung
    Frederick Cheung
    • 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 ?

    August 21st, 2008 @ 09:06 PM

  2. Frederick Cheung
    Frederick Cheung

    Oops, not sure what happened with the tags there! I bashed in your code against edge and 2.1 and couldn't reproduce it

    August 21st, 2008 @ 09:12 PM

  3. josh
    josh
    • State changed from new to wontfix

    November 22nd, 2008 @ 06:49 PM