How to remove/hide duplicate entry

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

Have a look at the uniq query method. See the Rails Guide on ActiveRecord Query Interface and search for uniq.

Colin

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

Colin Law wrote in post #1044737:

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.

That could certainly be the case. I also assumed the table example shown must have been an oversimplification of what the OP was actually trying to do. I was just attempting to clarify the implications of using uniq in the context of an ORM.

It's important to understand that using .uniq creates ambiguity. The objects in the resulting array no longer represent unique rows in the table. As such, those objects should not be used for updating the database.