Skip to content

Instantly share code, notes, and snippets.

@michaeldhopkins
Created February 7, 2012 18:42
Show Gist options
  • Save michaeldhopkins/1761169 to your computer and use it in GitHub Desktop.
Save michaeldhopkins/1761169 to your computer and use it in GitHub Desktop.
why aren't these returning unique?
ruby-1.9.2-p290 :056 > Sermon.where("date > ?", Date.today - 365).select(:speaker_id).uniq
Sermon Load (0.5ms) SELECT speaker_id FROM "sermons" WHERE (date > '2011-02-07')
=> [#<Sermon speaker_id: 1>, #<Sermon speaker_id: 1>, #<Sermon speaker_id: 1>, #<Sermon speaker_id: 1>, #<Sermon speaker_id: 1>]
Update: answer:
ruby-1.9.2-p290 :070 > Sermon.select(:speaker_id).where("date > ?", Date.today - 365).group(:speaker_id)
Sermon Load (0.5ms) SELECT speaker_id FROM "sermons" WHERE (date > '2011-02-07') GROUP BY speaker_id
=> [#<Sermon speaker_id: 1>]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment