Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

`uniq` in has-many based relations is order-dependent #8270

Closed
RKushnir opened this Issue · 1 comment

2 participants

@RKushnir

I believe the following behaviour is inconsistent. Here's the code:

class Post < ActiveRecord::Base
  has_many :comments
end

post.comments.where(author: 'bob').uniq produces a valid SQL-query with DISTINCT, whereas post.comments.uniq.where(author: 'bob') says NoMethodError: undefined method 'where' for #<Array:0x...>.
Also post.comments.scoped.uniq.where(author: 'bob') works as the former case.

@steveklabnik
Collaborator

This is how things work. If you call uniq, it converts to an array and then uniqs it. Arrays do not have a where method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.