针对Child的ActiveRecord查询,按祖父母排序(ActiveRecord Query for Child, Sort by Grandparent)

编程入门 行业动态 更新时间:2024-10-14 10:38:42
针对Child的ActiveRecord查询,按祖父母排序(ActiveRecord Query for Child, Sort by Grandparent)

我看到大量使用父元素上的属性进行排序的例子,但没有祖父母的东西。 如果有另外一篇文章详细说明,我找不到它,也许是因为我不知道这个词。 那么,这是我的理论模型:

class GrandParent < ActiveRecord::Base has_many :parents has_many :children, through: :parents end class Parent < ActiveRecord::Base belongs_to :grand_parent has_many :children end class Child < ActiveRecord::Base belongs_to :parent end

所以,我正在尝试提供一份儿童名单,但我希望按照祖父母的方式排序。

我正在尝试像Child.joins(:grandparent).order('grandparent.name').all这样无用的东西Child.joins(:grandparent).order('grandparent.name').all

但那只是不适合我。 我已尝试过其他一百种变体,但我不能说他们背后有太多的逻辑......任何人都对这个有任何好的想法?

I'm seeing tons of examples of using attributes on a parent element for sorting, but nothing for grandparent. If there's another post out there detailing this, I couldn't find it, maybe because I don't know the word for it. So, here are my theoretical models:

class GrandParent < ActiveRecord::Base has_many :parents has_many :children, through: :parents end class Parent < ActiveRecord::Base belongs_to :grand_parent has_many :children end class Child < ActiveRecord::Base belongs_to :parent end

So, I'm trying to present a list of children, but I want it sorted by grandparents.

I was trying useless things like Child.joins(:grandparent).order('grandparent.name').all

But that's just not doing it for me. I've attempted a hundred other variants, but I can't say they had much logic behind them... Anyone have any good thoughts on this one?

最满意答案

你可以试试

Child.joins(:parent => :grand_parent).order('grand_parents.name')

You could try

Child.joins(:parent => :grand_parent).order('grand_parents.name')

更多推荐

本文发布于:2023-08-07 07:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1462877.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:祖父母   ActiveRecord   Child   Grandparent   Sort

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!