【发布时间】:2018-11-21 19:07:27
【问题描述】:
如果我有以下模型,我如何返回用户创建的所有报告,但只返回每个玩家的“最高评分”报告?
class Player < ApplicationRecord
has_many :reports
end
class Report < ApplicationRecord
belongs_to :author
belongs_to :grade
belongs_to :player
end
class Grade < ApplciationRecord
has_many :reports
end
class Author < ApplicationRecord
has_many :reports
end
示例数据:
/Player/ - /Author/ - /Report Grade/
John Smith - David - 5
John Smith - David - 4
Thomas Li - David - 5
Mike Lee - Sean - 9
Mike Lee - Sean - 2
Arnold Jackson - Sean - 5
Cathleen Miller - Sean - 7
我想要的结果:
/Player/ - /Author/ - /Report Grade/
John Smith - David - 5
Thomas Li - David - 5
Mike Lee - Sean - 9
Arnold Jackson - Sean - 5
Cathleen Miller - Sean - 7
目前我正在使用以下内容:
Report.joins(:player).where(type: %w(spring fall))
我不确定如何过滤掉“评分较低”的记录。如果我需要包含更多信息,请告诉我。
【问题讨论】:
-
你真的需要为
Grade单独的表吗?如果你只是在reports上使用一个整数会简单得多。 -
@max,是的,等级值比此处显示的更复杂。我只是为了说明而简化了它。
标签: ruby-on-rails activerecord ruby-on-rails-5