我在一个页面(Rails视图)上显示作者,如下所示:
Author.where(featured: true).where.not(username: nil).order(:priority, :name)
个
然而,我最近添加了一个新的型号Photographer
,我想结合摄影师和作者,并以类似的方式对他们进行排序.它们共享大部分相同的列.
我试过了:
combined = (Author.where(featured: true).where.not(username: nil) + Photographer.where(featured: true).where.not(username: nil))
combined.order(:priority, name)
=> NoMethodError (undefined method `order' for #<Array:0x00005573bc2ab3b0>)
combined.sort_by {|item| [item.priority, item.name]}
=> ArgumentError (comparison of Array with Array failed)
需要注意的是,在某些情况下,priority
相当于nil
.因此,在这种情况下,它应该按名称排序.
如有任何帮助,我们不胜感激!