基于关联的 Rails 顺序

发布于 2024-09-30 22:08:12 字数 180 浏览 7 评论 0原文

我有一个问题和一个解决方案模型。问题有很多解决方案,解决方案属于问题。

我需要得到最近解决的问题,这意味着,得到最后的X个解决方案并得到它的问题。我很确定有一种方法可以使用named_scopes 来做到这一点,但我无法弄清楚。

非常感谢任何帮助:-)

谢谢,

尼古拉斯·霍克一世。

I have a Problem and a Solution model. Problem has many solutions and Solution belongs to problem.

I need to get the recently solved problem, that means, get the last X solutions and get it's problem. I'm pretty sure there's a way to do this with named_scopes but I can't figure it out.

Any help is really appreciated :-)

Thanks,

Nicolás Hock I.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不顾 2024-10-07 22:08:13

我实际上让named_scope工作了:

named_scope :solved_recently, :joins => :solutions, :order => "solutions.created_at DESC"
# Problem.solved_recently

无论如何谢谢@Jacob:)

I actually got the named_scope working:

named_scope :solved_recently, :joins => :solutions, :order => "solutions.created_at DESC"
# Problem.solved_recently

Thanks anyway @Jacob :)

归途 2024-10-07 22:08:13

我认为这应该有效:

x = #some number
solutions = Solution.find(:all, :order => ['created_at desc'], :limit => x)
problems  = solutions.map {|solution|
   solution.problem
}

I think this should work:

x = #some number
solutions = Solution.find(:all, :order => ['created_at desc'], :limit => x)
problems  = solutions.map {|solution|
   solution.problem
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文