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

发布于 2024-09-16 17:41:25 字数 591 浏览 7 评论 0原文

我认为我的措辞正确...

我有一个名为 asset 的模型,它是多态的:

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 的资产列表属性不为空,并按该 approved_at 属性按降序排列,上限为 50。我承认我不确定我所拥有的与正确性有多接近,但我的错误现在得到的是:

"Can not eagerly load the polymorphic association :assetable"

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"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月下凄凉 2024-09-23 17:41:25

做到这一点的正确方法是以不同的方式思考它,例如:

# 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文