假设有一个预订系统来预订某个活动的时间段。
我有一个事件模型和一个 time_slot 模型。每个事件都有man time_slots,每个槽属于一个事件
class Event < ActiveRecord::Base
has_many :time_slot
end
class TimeSlot < ActiveRecord::Base
belongs_to :event
end
当我执行Event.find(some_id)
如何根据模型上的字段(例如“slot_time”)对返回的时隙进行排序时
像这样:
Event.find(some_id).time_slot.order(:slot_time)
time_slot
实际上并不返回所有关联的时间段,它返回一个 ActiveRecord::Relation,就像TimeSlot.where(booked: true)
会那样。因此,您可以将更多查询方法链接到此关系。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句