Named_scope在轨唯一的记录? [英] Named_scope in rails unique records?

查看:121
本文介绍了Named_scope在轨唯一的记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时有可能有 named_scope 返回的记录唯一的某列?

Is it possible to have named_scope return records unique for a certain column?

例如

named_scope :unique_styles, :order =>"title desc", :limit => 3

这会给我三样式,但如果我想确保标题是不同的?在这种情况下,可能有三个记录使用相同的风格,我想这named_scope为仅标题的唯一值。

That will give me three styles but what if I want to make sure the title is different? In this case there may be three records with the same style, I want this named_scope to only give unique values of title.

所以 [风格1,款式1,款式1] 是不可能的,它会迫使自己放弃 [风格1,某些样式2,也许再过3]

So ["style 1", "style 1", "style 1"] isn't possible, it'll force itself to give ["style 1", "some style 2", "maybe another 3"]

  • 我觉得可以做到这一点,我使用的是现在。如果任何人有任何意见,无论那会是巨大的。
  • i think group may do it and I'm using that for now. If anyone has any comments regardless that'd be great.

推荐答案

您可能想要探索:为发现者和named_scopes组选项:

You probably want to explore the :group option for finders and named_scopes:

named_scope :unique_styles, :order => "title desc", :limit => 3, :group => "title"

这篇关于Named_scope在轨唯一的记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆