开发者

How to query this in Activerecord

I have a model (say Requirement), which has three foreign keys model1_id, model2_id and model3_id.

Other than these, I have a 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.

I want to get the marks for the last test in a subject that the student has given.

Just the last recent test of a given subject.


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜