Here's the example:
$ ruby script/generate scaffold thing name:string
class Thing < ActiveRecord::Base
  named_scope :distinct, :group => :name
end

:select => "DISTINCT things.*" would work in the same way.

Start up script/console:

>> Thing.new(:name => "a").save!
=> true
>> Thing.new(:name => "a").save!
=> true
>> Thing.all
=> [#<Thing id: 1, name: "a", created_at: "2009-10-16 02:34:28",
updated_at: "2009-10-16 02:34:28">, #<Thing id: 2, name: "a",
created_at: "2009-10-16 02:34:28", updated_at: "2009-10-16 02:34:28">]
>> Thing.distinct
=> [#<Thing id: 2, name: "a", created_at: "2009-10-16 02:34:28",
updated_at: "2009-10-16 02:34:28">]
>> Thing.distinct.size
=> 2
>> Thing.distinct.all.size
=> 1

As you can see, Thing.distinct.size is mis-reporting the size of the
result. I can't figure out what's causing this, or why adding .all
fixes it. Running the query straight into the database predictably
returns just one record:

sqlite> SELECT * FROM "things" GROUP BY name;
        id = 2
      name = a
created_at = 2009-10-16 02:34:28
updated_at = 2009-10-16 02:34:28

Am I missing something obvious?

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to