0
當我在每個塊中使用卡車對象調用.deliveries.last時,rails不會爲該卡車返回具有最高ID的交貨。Rails方法返回不同的響應
@todays_workdays = Workday.todays_workdays
@todays_workdays.each do |workday|
@working_trucks << workday.driver.truck
end
@working_trucks.each do |truck|
if truck.deliveries.last.time_arrived.between?(Time.zone.now.beginning_of_day,Time.zone.now.end_of_day) # returns a delivery for each truck but not its actual last delivery (e.g. id 447)
...
end
end
但是,當我使用Truck.find(truck.id),它工作正常,返回的最後交付每輛卡車
if Truck.find(truck.id).deliveries.last.time_arrived.between?(Time.zone.now.beginning_of_day,Time.zone.now.end_of_day) # returns proper last delivery for each truck (e.g. id 486)
...
end
我不知道爲什麼會這樣。任何幫助/解釋將不勝感激。謝謝!
感謝您的小費,但是我使用Rails 4中。另外,我嘗試使用.sort_by {| X | x.id},同樣的問題依然存在。 – avalente1