Rails collection_select 修改输出

发布于 2024-12-19 02:24:05 字数 676 浏览 1 评论 0原文

在其中一个视图(erb)中,我有以下代码

collection_select(:wp_article, :wp_site_id, WpSite.find_by_sql(["SELECT wp_sites.* FROM wp_sites, users_wp_sites, users WHERE users_wp_sites.user_id = users.id and users_wp_sites.wp_site_id = wp_sites.id and username= ?", session[:cur_username]]), :id, :name, 

{:prompt => true})

这在下拉列表中列出了当前用户(cur_username)可以访问的站点名称,例如

Google.com
Facebook.com
Twitter.com

现在我需要显示用户访问的次数这些站点位于同一下拉列表中,如下所示:

Google.com (32)
Facebook.com (68)
Twitter.com (21)

如何修改查询以包含计数和站点名称? (我知道如何在上面发布的同一查询中获取子查询中的计数,但不知道如何将站点名称(:name)和计数(我将在子查询中获得的新计数)传递到下拉列表作为一个完整的字符串。)

In one of the views (erb) I have the following code

collection_select(:wp_article, :wp_site_id, WpSite.find_by_sql(["SELECT wp_sites.* FROM wp_sites, users_wp_sites, users WHERE users_wp_sites.user_id = users.id and users_wp_sites.wp_site_id = wp_sites.id and username= ?", session[:cur_username]]), :id, :name, 

{:prompt => true})

This lists the site names that are accessible by the current user (cur_username) in a drop down list, for example

Google.com
Facebook.com
Twitter.com

Now I need to display the number of times that the user has accessed these sites in the same drop down list, like this:

Google.com (32)
Facebook.com (68)
Twitter.com (21)

How can I modify the query to include the count together with the site name?
(I know how to get the count in a subquery within the same query posted above, but do not know how to pass name of site (:name) and count (the new count I will get in the subquery) to the drop down list as one complete string.)

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

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

发布评论

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

评论(1

春夜浅 2024-12-26 02:24:05

设法以这种方式解决它:

<%= collection_select(:wp_article, :wp_site_id, WpSite.find_by_sql(["SELECT wp_sites.id, CONCAT(wp_sites.name, ' (', (select count(*) from wp_articles where wp_articles.wp_site_id = wp_sites.id and wp_articles.author = users.username and published = 1), ')') name FROM wp_sites, users_wp_sites, users WHERE users_wp_sites.user_id = users.id and users_wp_sites.wp_site_id = wp_sites.id and username= ?", session[:cur_username]]), :id, :name, {:prompt => true}) %>

在主 SQL 查询中,我使用 CONCAT (它是 MySQL 后端)将计数附加到名称并保留属性名称“name”(以便通过符号 :name 引用它) 。

Managed to solve it in this way:

<%= collection_select(:wp_article, :wp_site_id, WpSite.find_by_sql(["SELECT wp_sites.id, CONCAT(wp_sites.name, ' (', (select count(*) from wp_articles where wp_articles.wp_site_id = wp_sites.id and wp_articles.author = users.username and published = 1), ')') name FROM wp_sites, users_wp_sites, users WHERE users_wp_sites.user_id = users.id and users_wp_sites.wp_site_id = wp_sites.id and username= ?", session[:cur_username]]), :id, :name, {:prompt => true}) %>

In the main SQL query I am using CONCAT (it is a MySQL backend) to append the count to the name and keeping the attribute name "name" (so that it is referenced by symbol :name).

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