0
class User < ActiveRecord::Base
has_many :events
has_many :trainings, -> { distinct }, through: :events
end
class Event < ActiveRecord::Base
belongs_to :user
belongs_to :training
end
class Training < ActiveRecord::Base
has_many :events
has_many :user, -> { distinct }, through: :events
end
events table contains:
t.integer "user_id"
t.integer "training_id"
t.due "date"
想象的事件表具有以下USER_ID,training_id和到期日首先選擇在軌道的每一個組唯一的密鑰對的日期
[ 1, 1, 25/06/16]
[ 1, 1, 25/06/15]
[ 1, 1, 25/06/14]
[ 1, 2, 25/06/16]
[ 1, 2, 25/06/15]
[ 1, 2, 25/06/14]
[ 2, 1, 25/06/16]
[ 2, 1, 25/06/15]
[ 2, 1, 25/06/14]
[ 2, 2, 25/06/16]
[ 2, 2, 25/06/15]
[ 2, 2, 25/06/14]
我想選擇按到期日最近的值這些「組」中的每一個(用戶&訓練密鑰對)。
即
[ 1, 1, 25/06/16]
[ 1, 2, 25/06/16]
[ 2, 1, 25/06/16]
[ 2, 2, 25/06/16]
任何想法如何實現這一目標?
嘗試'Event.order(由於:DESC)' – Pavan
這不工作我害怕 - 它返回所有的人通過適當date.'code' [1,1,25/06有序/ 16],[1,2,25/06/16],[2,1,25/06/16],[2,2,25/06/16],[1,1,25/06/15 ],[1,2,25/06/15]等 – jameslagan