通过对另一个表中的值进行分组来对表进行排序

发布于 2024-10-17 08:48:34 字数 578 浏览 4 评论 0原文

我有一个问题表:标题 每个问题都有答案,在另一个表中:text,question_id 每个答案都有投票,在另一个表中:answer_id

我可以询问总票数,

question.votes.count

我已经看到如何对投票数据库进行分组 http://guides.rubyonrails.org/active_record_querying.html#group

Vote.group("answer_id")

但我想通过投票对我的问题进行排序。即通过另一个表的分组对一个表中的数组进行排序。这可能吗?

q=Question.last
q.answers.order_by_vote #How to do this?

我认为我的答案可能是对答案模型进行一个范围,对属于该问题的投票子集进行分组。我说得对吗?

谢谢。

I have a table of questions: title
Each question has answers, in another table: text, question_id
Each answer has votes, in another table: answer_id

I can ask for total votes with

question.votes.count

I have seen how to group the votes db at
http://guides.rubyonrails.org/active_record_querying.html#group

Vote.group("answer_id")

But I want to order my questions by votes. That is ordering an array from a table via the grouping of another table. Is that possible?

q=Question.last
q.answers.order_by_vote #How to do this?

I think my answer could be doing a scope on the answer model that makes the grouping on the subset of votes that belong to the question. Am I right?

Thank you.

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

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

发布评论

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

评论(2

笔落惊风雨 2024-10-24 08:48:34

首先,我认为当您说 DB(数据库)时,您指的是表。表是数据库内保存特定模型数据的结构,例如(问题、投票和答案)。在您的情况下,您有三个表。

其次,调用 attr_accessible :answer_id 不会使属性可通过索引搜索。它是无论您是否声明它是可访问的,都可以通过此属性进行搜索,除非您将其指定为数据库中的索引,否则可以使用批量分配来设置该属性,例如,当您调用 update_attributes 时。

最后,如果您想按answer_id进行分组并按投票数进行排序,则可以使用以下查询来实现:

Vote.select('count(id) as votes, answer_id').group('answer_id').order('votes DESC')

如果您想按分组所依据的其他值进行排序,则必须添加该值这也确保您不会获得第二个值的任意结果。

Vote.group('answer_id').group('column_1').order('column_1 DESC')

First, I think that you mean table when you say DB (database). A table a structure inside a database that holds data for a specific model, for example (questions, votes and answers. In your case you have three tables.

Second, calling attr_accessible :answer_id does not make an attribute searchable by an index. It is searchable by this attribute regardless of whether or not you declare it accessible. It is not an index unless you specify it as an index in the database. attr_accessible means that the attribute can be set using mass-assignment, for example when you call update_attributes.

Finally, if you want to group by answer_id and order by the number of votes, you can do so with the following query:

Vote.select('count(id) as votes, answer_id').group('answer_id').order('votes DESC')

If you want to order by some other value than the one you are grouping by, you'll have to add that to your group as well. This ensures that you aren't getting arbitrary results for the second value. For example:

Vote.group('answer_id').group('column_1').order('column_1 DESC')
做个少女永远怀春 2024-10-24 08:48:34

我刚刚找到了一种按投票数组织链接的方法,使用链接视图中的排序方法:

<% @user.links.sort { |a, b| b.votes.sum(:分数) <=> a.votes.sum(:score) }.each do |link| %>

votes.sum(:score) 从投票表中获取特定链接的投票数。

希望有帮助。

I just figured out a way to organize my links by vote count, using the sort method in my links view:

<% @user.links.sort { |a, b| b.votes.sum(:score) <=> a.votes.sum(:score) }.each do |link| %>

votes.sum(:score) grabs the number of votes a particular link has from the votes table.

hope that helps.

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