Rails collection_select 修改输出
在其中一个视图(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
设法以这种方式解决它:
在主 SQL 查询中,我使用 CONCAT (它是 MySQL 后端)将计数附加到名称并保留属性名称“name”(以便通过符号 :name 引用它) 。
Managed to solve it in this way:
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).