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

Closed
RKushnir opened this Issue Nov 19, 2012 · 1 comment

Projects

None yet

2 participants

@RKushnir
Contributor

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.

Member

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