Mat*_*ias 29 activerecord ruby-on-rails
有人可以简要地向我解释的方法之间使用的差异uniq和distinct?
我已经看到两者都在类似的背景下使用,但差异对我来说并不十分清楚.
its*_*lay 60
导轨查询像阵列的行为,从而.uniq产生相同的结果为.distinct,但
.distinct 是SQL查询方法.uniq 是数组方法注意:在Rails 5+ Relation#uniq中已弃用,建议使用Relation#distinct.见http://edgeguides.rubyonrails.org/5_0_release_notes.html#active-record-deprecations
提示:
.includes在通话之前使用.uniq/.distinct会降低或加快您的应用,因为
uniq 不会产生额外的SQL查询distinct 会做但两种结果都是一样的
例:
users = User.includes(:posts)
puts users
# First sql query for includes
users.uniq
# No sql query! (here you speed up you app)
users.distinct
# Second distinct sql query! (here you slow down your app)
Run Code Online (Sandbox Code Playgroud)
这对于制作高性能应用程序非常有用
提示:
同样适用于
.sizevs .count;present? VS .exists?pluck VS map小智 7
Rails 5.1 已从 Activerecord Relation 中删除了 uniq 方法并添加了 unique 方法...
undefined method subscribed for Array)| 归档时间: |
|
| 查看次数: |
15824 次 |
| 最近记录: |