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 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 8 February 2012 08:19, Jade Zallao <jadezallao-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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?Have a look at the uniq query method. See the Rails Guide on ActiveRecord Query Interface and search for uniq. 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 > endAlso 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. -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 8 February 2012 13:21, Robert Walker <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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. -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.