【发布时间】:2023-04-05 08:37:02
【问题描述】:
我有以下型号:
#equipment.rb
class Equipment < ActiveRecord::Base
belongs_to :odometer_type
has_many :odometers
end
#odometer.rb
class Odometer < ActiveRecord::Base
belongs_to :equipment
# I am currently doing this to find the last mileage entered (which is wrong too cause I want the mileage from the max date entered)
def self.current_reading(equipment)
all.where(:equipment_id => equipment.id).max(:mileage)
end
end
这在视图中看起来更糟,像这样:
= @equipment.odometers.current_reading(@equipment)
我认为应该有更好的方法来做到这一点,但我似乎无法想出或找到任何东西。老实说,我什至不知道如何搜索这样的东西。
感谢您的帮助。
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3.1 associations max