hi, how can I make sure that ''customer_student'' won''t be pushed twice to the hash in the following example ? @potential_course_students = [] @selected_customers.each do |customer| customer.customer_student.each do |customer_student| if customer_student.display_name.include?(params[:term]) && ! @potential_course_students.include?(customer_student) @potential_course_attendees << customer_student end end end unless params[:term].blank? appreciate any help. Thank you Dani -- 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 03 Dec 2011, at 17:22, Dani Dani wrote:> how can I make sure that ''customer_student'' won''t be pushed twice to > the > hash in the following example ? > > @potential_course_students = [] > @selected_customers.each do |customer| > customer.customer_student.each do |customer_student| > if customer_student.display_name.include?(params[:term]) && > ! @potential_course_students.include?(customer_student) > @potential_course_attendees << customer_student > end > end > end unless params[:term].blank?Since you''re pushing objects into an array, you can just "uniq!" it: http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-uniq-21 I do have to add that you''re doing a lot in the controller that seems to be model-related. You might want to encapsulate functionality a bit better. Best regards Peter De Berdt -- 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 Sat, Dec 3, 2011 at 8:22 AM, Dani Dani <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> how can I make sure that ''customer_student'' won''t be pushed twice to the > hash in the following example ?What hash?> @potential_course_students = [] > @selected_customers.each do |customer| > customer.customer_student.each do |customer_student| > if customer_student.display_name.include?(params[:term]) && > ! @potential_course_students.include?(customer_student)@potential_course_students is an empty array; how would it include anything at that point?> @potential_course_attendees << customer_student@potential_course_attendees isn''t defined in your snippet -- ??> end > end > end unless params[:term].blank?-- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- 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.
Peter De Berdt wrote in post #1034916:> > Since you''re pushing objects into an array, you can just "uniq!" it: > http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-uniq-21thank you for the answer. where should I set the "uniq!" exactly ? rgeards Dani -- 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.
Hassan Schroeder wrote in post #1034918:> On Sat, Dec 3, 2011 at 8:22 AM, Dani Dani <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > >> how can I make sure that ''customer_student'' won''t be pushed twice to the >> hash in the following example ? > > What hash? > >> @potential_course_students = [] >> @selected_customers.each do |customer| >> customer.customer_student.each do |customer_student| >> if customer_student.display_name.include?(params[:term]) && >> ! @potential_course_students.include?(customer_student) > > @potential_course_students is an empty array; how would it include > anything at that point? > >> @potential_course_attendees << customer_student > > @potential_course_attendees isn''t defined in your snippet -- ?? > >> end >> end >> end unless params[:term].blank? > > -- > Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > http://about.me/hassanschroeder > twitter: @hassanthe variable: @selected_customers includes already the potential students. didn''t show all code. Thanks Dani -- 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 Sat, Dec 3, 2011 at 9:27 AM, Dani Dani <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> the variable: @selected_customers includes already the potential > students. didn''t show all code.And I didn''t say anything about @selected_customers -- I said> @potential_course_attendees isn''t defined in your snippet -- ??(among other things) -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- 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.
Hassan Schroeder wrote in post #1034925:> On Sat, Dec 3, 2011 at 9:27 AM, Dani Dani <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > >> the variable: @selected_customers includes already the potential >> students. didn''t show all code. > > And I didn''t say anything about @selected_customers -- I said > >> @potential_course_attendees isn''t defined in your snippet -- ?? > > (among other things) > -- > Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > http://about.me/hassanschroeder > twitter: @hassansorry, @potential_course_attendees should be @potential_course_students Dani -- 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 Sat, Dec 3, 2011 at 9:52 AM, Dani Dani <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> @potential_course_attendees should be @potential_course_studentsSo is that a typo in your email, or a typo in your code? It does make a difference, you know :-) Hint: maybe you should copy/paste instead of retyping, or use gist (https://gist.github.com/) or equivalent if you want to preserve the original formatting *and* minimize confusion. -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- 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.
Hassan Schroeder wrote in post #1034929:> On Sat, Dec 3, 2011 at 9:52 AM, Dani Dani <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > >> @potential_course_attendees should be @potential_course_students > > So is that a typo in your email, or a typo in your code? > > It does make a difference, you know :-) > > Hint: maybe you should copy/paste instead of retyping, or use gist > (https://gist.github.com/) or equivalent if you want to preserve the > original formatting *and* minimize confusion. > > -- > Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > http://about.me/hassanschroeder > twitter: @hassanit was a typo in the e-mail. so how could I make sure no ''customer_student'' gets twice into the @potential_course_students hash. regards Dani -- 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 Sat, Dec 3, 2011 at 10:19 AM, Dani Dani <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> it was a typo in the e-mail. > so how could I make sure no ''customer_student'' gets twice into the > @potential_course_students hash.1) It''s not a hash, it''s an array. 2) Do you want to *prevent* it being added twice or do you just want to ensure that all customer_student instances are unique? If the latter, then Peter''s suggestion is fine; read the Array doc to see how to use that method. If the former -- it''s a little more work :-) HTH, -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- 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.
Hassan Schroeder wrote in post #1034937:> 2) Do you want to *prevent* it being added twice or do you just want > to ensure that all customer_student instances are unique? >want to prevent it being added twice. Hassan, I appreciate your help Dani -- 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 Sat, Dec 3, 2011 at 11:13 AM, Dani Dani <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> want to prevent it being added twice.Very simplistically -- class PotentialCourseStudents < Array def << (customer_student) raise ArgumentError, "already exists" if self.include?(customer_student) self.push(customer_student) ## alternately, ignore duplicates silently # self.push(customer_student) unless self.include?(customer_student) end end Note that there are more ways to add to an array than just `<<` so you need to evaluate whether this is going to work for you :-) Also, Ruby''s stdlib has a "Set" which is an *unordered* collection, but which does not allow duplicates. So if order''s not important, you could just go with @potential_course_students = Set.new Something to think about... HTH, -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- 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.