check hash content ?

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

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

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 &lt;&lt; customer\_student

@potential_course_attendees isn't defined in your snippet -- ??

Peter De Berdt wrote in post #1034916:

Since you're pushing objects into an array, you can just "uniq!" it: Class: Array (Ruby 1.9.3)

thank you for the answer.

where should I set the "uniq!" exactly ?

rgeards Dani

Hassan Schroeder wrote in post #1034918:

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 wrote in post #1034925:

So is that a typo in your email, or a typo in your code?

It does make a difference, you know :slight_smile:

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 wrote in post #1034929:

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 :slight_smile:

HTH,

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

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 :slight_smile:

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,