我有两个1-n关系的班级就像这样:
class Band
include Mongoid::Document
has_many :members
end
class Member
include Mongoid::Document
field :name, type: String
field :joined, type: Date
belongs_to :band
end
现在当我调用
band.members
时,我得到了成员对象我想要的是,如果我调用band.members.last
来获取最后加入的成员为此,我定义了<=>
的Member
方法,并根据joined
进行排序:band.members.sort.last
如何使此行为默认我不想避免额外的电话排序这有可能吗?如果有,怎么办?
最佳答案
class Band
include Mongoid::Document
has_many :members, :order => :joined.asc
end
关于ruby - 配置Mongoid关系以返回已排序的对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18980876/