How to query this in Activerecord
I have a model (say Requirement), which has three foreign keys model1_id, model2_id and model3_id
.
value
attribute in the Requirement
model.
In my requirements table
, I have multiple values for a single combination of mode开发者_如何学JAVAl1_id, model2_id and model3_id
.
I want to get all the requirements i.e. Requirement.all
, and there should be only a single result for every combination, where the single result is the one which was created recently.
How can I do that?
UPDATE(to clarify the question):
Think of it as A Student has many subjects and give many tests for each subject and the value column is marks.
Assuming you already have the @student loaded (and associations hooked up), then this should work (using Geography as a sample subject) :
@student.subject.find_by_name("geography").tests.order("created_at desc").limit(10).sum(:mark)
精彩评论