views:

17

answers:

1

I think I worded that correctly...

I have a model called asset which is polymorphic:

class Asset < ActiveRecord::Base
  belongs_to :assetable, :polymorphic => true
...
end

I have a class level method that acts as a scope:

def self.some_scope
     assets = Asset.joins(:assetable).where('assetable.approved_at IS NOT NULL').order('assetable.approved_at DESC').limit(50)
end

I am trying to get a list of assets who's parent's approved_at attribute is not null and order by that approved_at attribute in descending order with a limit of 50. I'll admit that I'm not sure how close what I have is to being correct but the error I am getting now is:

"Can not eagerly load the polymorphic association :assetable"
A: 

The proper way to do this is to think of it differently like:

# If assetable model is called Folder
def self.some_scope
   folders = Folder.where('approved_at IS NOT NULL').order('approved_at DESC').limit(50)
   folders.collect{|p| p.assets}
end
Tony