代码之家  ›  专栏  ›  技术社区  ›  Tony

如何按ActiveRecord中多态模型的父属性排序?

  •  0
  • Tony  · 技术社区  · 16 年前

    我想我说得对。。。

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

    我有一个作为作用域的类级方法:

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

    我想得到一份父母的资产清单 approved_at 批准 属性的降序,限制为50。我承认,我不确定自己有多接近正确,但我现在得到的错误是:

    "Can not eagerly load the polymorphic association :assetable"
    
    1 回复  |  直到 16 年前
        1
  •  0
  •   Tony    16 年前

    正确的方法是用不同的方式来思考:

    # 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
    
    推荐文章