开发者

How to order by an attribute of a parent for a polymorphic model in ActiveRecord?

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"


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜