我看到大量使用父元素上的属性进行排序的例子,但没有祖父母的东西。 如果有另外一篇文章详细说明,我找不到它,也许是因为我不知道这个词。 那么,这是我的理论模型:
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 endSo, 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')更多推荐
发布评论