Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在.where调用rails上添加唯一性_Ruby On Rails_Ruby_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 如何在.where调用rails上添加唯一性

Ruby on rails 如何在.where调用rails上添加唯一性,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,我在文档中找不到它,搜索它只会显示在模型中添加唯一性的结果 如何为.where调用添加唯一性。我希望下面的命令不使用相同的类别项目id抓取CategoryItemValues last_updated_items = CategoryItemValue.where(guide_id: @guide.id).order(updated_at: :desc).limit(5) 因此,该命令获取5个最近更新的CategoryItemValues,而不获取5个具有相同category\u item\u

我在文档中找不到它,搜索它只会显示在模型中添加唯一性的结果

如何为
.where
调用添加唯一性。我希望下面的命令不使用相同的
类别项目id
抓取
CategoryItemValues

last_updated_items = CategoryItemValue.where(guide_id: @guide.id).order(updated_at: :desc).limit(5)
因此,该命令获取5个最近更新的
CategoryItemValues
,而不获取5个具有相同
category\u item\u id的值。如果
category\u item\u id
与另一个最近更新的相同,它将跳过它,并将下一个最近更新的添加到返回的5个结果中

更新

由于某些原因,使用
select
方法和
distinct
执行此操作仍然会产生重复的
category\u item\u id

CategoryItemValue.选择(:category\u item\u id).distinct.where(guide\u id:@guide.id).order(updated\u at::desc).limit(5).pull(:category\u item\u id,:updated\u at)

[2016年5月11日星期三10:24:39 UTC+00:00],[2016年5月11日星期三10:21:20 UTC+00:00],[2016年5月11日星期三10:20:43 UTC+00:00],[2016年5月11日星期三09:14:14 UTC+00:00],[2016年5月11日星期三09:14:04 UTC+00:00]


请注意,第二个和第三个数组值的开头都是3。(另外,我不确定日期时间后的逗号是否会影响从数组中提取这些值)

您可以将SQL传递给活动记录
where
子句。您正在寻找的方法是
DISTINCT

您还可以使用rails distinct方法

请尝试:
CategoryItemValue。选择(:category\u id)。不同。其中(guide\u id:@guide.id)


这个答案也可能有帮助:

试试这样的方法:

CategoryItemValue.select(:category_item_id, "MIN(updated_at) as updated_at").distinct.where(guide_id: @guide_id).group(:category_item_id).order(updated_at: :desc).limit(5)

这两种方法都将按类别项目id设置的结果分组,并应为每个类别项目返回一条记录,其中该类别项目的
updated\u的最早(MIN)值位于
。要获取最新更新的记录,请使用MAX而不是MIN.

@CharlesHamel如何将其添加到此命令中?last\u updated\u items=CategoryItemValue.where(guide:@guide)。uniq我还想说,您的var名称不正确,因为它是uniq,您应该将其重命名为last\u updated\u item,而不使用“s”.uniq仅表示如果两条记录相同,则将删除一条记录。在这种情况下,所有记录都是唯一的,因为
updated\u at
列始终是唯一的,考虑到它一直到第二次更新,我需要类似.uniq的内容,但仅在一列
category\u item\u id
上尝试添加。选择?它是从中选择a、b、c并对itI进行uniq,我正在传递
.distinct
给它,但它没有任何区别,好像它甚至不在那里
CategoryItemValue.where(guide\u id:@guide.id).distinct(:category\u item\u id).order(更新地址::desc).limit(5)
我实际上是指SQL,但这也应该有效。请尝试以下操作:
CategoryItemValue。选择(:guide\u id).distinct.where(guide\u id:@guide.id)
不会
select
只需获取
guide\u id
?不是自己给我带来唱片吗?是的,但是你可以拿到剩下的唱片。例如,你可以拔掉身份证。或者你可以添加一个arg来选择。这是我所拥有的,但再一次
。distinct
仍然允许通过重复的
类别项目\u id
CategoryItemValue.选择(:category\u item\u id,:updated\u at).distinct(:category\u item\u id).order(updated\u at::desc).limit(5)
如何从中获取每个记录的
updated\u at
值?@Rob我刚刚更新了查询,为第二列添加了一个别名,并将其命名为
updated\u at
。您应该能够在
处将其称为
updated\u,就像现在的正常情况一样。这是我最初的回答中的疏忽。