如何显示rails select语句中的所有属性?(How do I display all attributes from a rails select statement?)

编程入门 行业动态 更新时间:2024-10-26 04:32:24
如何显示rails select语句中的所有属性?(How do I display all attributes from a rails select statement?)

请问是否可以知道如何在下面的代码中使用哈希显示:user_id,:player_id和:amount属性?

目前创建的数组返回player_id作为键,并返回值{1695 => 100,1714 => 200},而我需要像{1695 => {:user_id,100}这样的东西?

控制器中的代码如下:

@duplicates = Bid.select(:player_id, :user_id, :amount).group(:player_id).having("count(*) > 1").maximum(:amount)

视图中的代码:

<div class="col-sm-3" style="background-color:white;"> <%= @duplicates %><br> </div>

Please would it be possible to know how to display the :user_id, :player_id and :amount attributes together in a hash from the below code?

At the moment the array created returns player_id as the key and amount as the value {1695=>100, 1714=>200} whereas I need something like {1695=>{:user_id,100}?

The code in the controller is as follows:

@duplicates = Bid.select(:player_id, :user_id, :amount).group(:player_id).having("count(*) > 1").maximum(:amount)

The code in the view:

<div class="col-sm-3" style="background-color:white;"> <%= @duplicates %><br> </div>

最满意答案

我将在主帖的评论中添加我们提出的答案:

@duplicates = Bid. select("player_id, user_id,MAX(amount)"). group(:player_id). having("count(*) > 1")

现在根据您的用例装饰结果:

@duplicates.map! do |rec| { rec. player_id => [ rec.user_id, rec.amount ] } end

I am going to add the answer which we came up from the conversations in comments of the main post:

@duplicates = Bid. select("player_id, user_id,MAX(amount)"). group(:player_id). having("count(*) > 1")

Now decorate the result as per your usecase:

@duplicates.map! do |rec| { rec. player_id => [ rec.user_id, rec.amount ] } end

更多推荐

本文发布于:2023-04-27 23:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329640.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语句   属性   select   rails   attributes

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!