假設我有兩個型號,submission
has_many
submissionstate
如何在Rails 3中重寫這個SQL查詢?
和表submissionstates
有以下欄目:
id | submission_id | state_id | created_at
和查詢
SELECT submission_id, state_id
FROM submissionstates ss
JOIN (
SELECT MAX(created_at) as created_at
FROM submissionstates ss
GROUP BY submission_id
) x
ON ss.created_at = x.created_at
WHERE state_id = 0
上查詢RoR的文檔,應該可以幫助您http://guides.rubyonrails.org/active_record_querying.html – 2013-03-06 15:37:30
參考 - http://stackoverflow.com/questions/7016422/how-to-convert-sql-query-to-rails-active-record-query – 2013-03-06 15:44:49