On 8 February 2012 13:21, Robert Walker <li...@ruby-forum.com> wrote: > Jade Zallao wrote in post #1044699: >> Im currently new in rails and using v 2.3.11 >> >> I would like to ask if how can I query entries without including their >> duplicates? >> >> class CreatesStudent < ActiveRecord::Migration >> def self.up >> create_table :students do |t| >> t.string :first_name >> t.string :middle_name >> t.string :last_name >> t.timestamps >> end >> end >> >> def self.down >> drop_table :profiles >> end >> end > > Also worth noting that if you did have duplicates in the table you show > here then you'd be saying that you have two people with exactly the same > name. Possible of course, but without additional detail you would have a > tough time knowing which one was which. > > Doing a uniq query on this table would probably be a bad idea. You would > have one result that represents two, or more, different people and you > would have no way to know which one was which. > > By default Rails will automatically provide a unique index in the form > of an auto-incrementing integer id column. It uses that as the primary > key for the table. A normal query will include that column so there is > generally no need for distinct queries (i.e. SELECT DISTINCT). > > On the other hand, if you actually have duplicate records that represent > the same person then you've got a bad design problem that you should fix > right way. > > General rule of thumb is that distinct queries should be fairly rare in > ORM based frameworks. The mapping could get easily confused by not > knowing which object represents which row in the table.
I had assumed (quite possibly wrongly) that the OP wanted to find, for example, all unique last_names, for example, in which case I think he could do something like Student.select(:last_name).uniq but if this is not the case then Robert is correct. Colin -- 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 rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.