如何在Activerecord中查询这个
我有一个模型(比如需求),它有三个外键 model1_id、model2_id 和 model3_id
。
除此之外,我的 Requirement
模型中有一个 value
属性。
在我的需求表
中,model1_id、model2_id 和 model3_id
的单个组合有多个值。
我想获得所有需求,即 Requirement.all
,并且每个组合应该只有一个结果,其中单个结果是最近创建的结果。
我怎样才能做到这一点?
更新(澄清问题):
将其视为学生有许多科目,并为每个科目进行多次测试,值列是分数。
我想获得学生所给出的科目的最后一次测试的分数。
只是给定主题的最后一次测试。
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 model1_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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您已经加载了@student(并连接了关联),那么这应该可以工作(使用地理作为示例主题):
Assuming you already have the @student loaded (and associations hooked up), then this should work (using Geography as a sample subject) :