Rails 3 查询接口:使用关联模型

发布于 2024-09-28 06:11:25 字数 903 浏览 5 评论 0原文

我将使用通用博客示例。

class Post < ActiveRecord::Base
  has_many :comments
end
class Comment < ActiveRecord::Base
  belongs_to :post
end

当查询Post时,如何访问它的关联(即:comments)?

这应该是世界上最简单的事情,但我还没有找到任何关于它的文档。甚至http://edgeguides.rubyonrails.org/3_0_release_notes.html#query-interfacehttp://m.onkey.org/2010/1 /22/active-record-query-interface 毫无帮助,基本上是说“现在有像 joins 和 include 这样的方法可以做与 SQL 语句相同的事情。”是的,谢谢。

所以这里有一些我想做的非常简单的事情,虽然行不通,但应该是显而易见的我想要完成的事情:

Post.where(:comments.count >= 10)
Post.where(:comments.author_id == current_user.id)
Post.order(:comments.count)

我们如何才能做到这些而不诉诸带有 SQL 味道的 ruby​​ 代码(从而违背了 Active Record 的目的) )? 谢谢 :)

I'll use the generic blog example.

class Post < ActiveRecord::Base
  has_many :comments
end
class Comment < ActiveRecord::Base
  belongs_to :post
end

When querying Post, how do you access its associations (i.e. :comments)?

This should be the easiest thing in the world, but I haven't found any documentation on it. Even http://edgeguides.rubyonrails.org/3_0_release_notes.html#query-interface and http://m.onkey.org/2010/1/22/active-record-query-interface were unhelpful, basically saying "Now there are methods like joins and includes to do the same thing as SQL statements." yeah thanks.

So here are very straightforward things I want to do, which do not work, but should be obvious what I'm trying to accomplish:

Post.where(:comments.count >= 10)
Post.where(:comments.author_id == current_user.id)
Post.order(:comments.count)

How can we do these without resorting to ruby code that reeks of SQL (thus defeating the purpose of Active Record)?
Thanks :)

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

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

发布评论

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

评论(2

椒妓 2024-10-05 06:11:25

如果你在Post上设置了counter_cache,你可以直接看到评论它有多少条评论而无需查询,这使得它变得更容易和更快。

这将解决您提出的第一个问题和最后一个问题。

然后您可以像这样查询它们,

  Post.where(:comments_count >= 10)
  Post.order(:comments_count)

但最好为此设置范围。

我不确定你想对第二个问题做什么,你想显示当前用户评论过的所有帖子吗?

If you set up a counter_cache on Post for the comments you can see directly how many comments it has without having to query, this makes it easier and faster.

This would take care of the first and last question you posed.

You can then query them like this,

  Post.where(:comments_count >= 10)
  Post.order(:comments_count)

But you are better off setting up scopes for that.

I'm not sure what you want to do with the second question, do you want to show all posts where the current user has commented on?

你在我安 2024-10-05 06:11:25

Post.where(:comments.count >= 10)

Post.find(:all).select{|p| p.comments.count >= 10)

Post.where(:comments.author_id == current_user.id)

Post.find(:all).select{|p| p.comments.select{|c| c.author_id == current_user.id } }

Post.order(:comments .count)

Yikes, this one beats me.

嘿,还有,我的两篇文章对 SQL 的描述有点重。我知道人们用 a 做得更优雅

Post.find(:all, :conditions => blah blah blah..

,但我不知道该怎么说。对不起。

Post.where(:comments.count >= 10)

Post.find(:all).select{|p| p.comments.count >= 10)

Post.where(:comments.author_id == current_user.id)

Post.find(:all).select{|p| p.comments.select{|c| c.author_id == current_user.id } }

Post.order(:comments.count)

Yikes, this one beats me.

Hey also, my two posts are kinda heavy on the SQL. I know people do it more elgantly with a

Post.find(:all, :conditions => blah blah blah..

But I'm not sure how to put that. Sorry.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文