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

On 03 Dec 2011, at 17:22, Dani D. wrote:

   end
 end

end unless params[:term].blank?

Since you’re pushing objects into an array, you can just “uniq!” it:

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

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

On Sat, Dec 3, 2011 at 8:22 AM, Dani D. [email protected] 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 S. ------------------------ [email protected]

twitter: @hassan

Hassan S. wrote in post #1034918:

On Sat, Dec 3, 2011 at 8:22 AM, Dani D. [email protected] 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 S. ------------------------ [email protected]
Hassan Schroeder | about.me
twitter: @hassan

the variable: @selected_customers includes already the potential
students. didn’t show all code.
Thanks
Dani

Hassan S. wrote in post #1034925:

On Sat, Dec 3, 2011 at 9:27 AM, Dani D. [email protected] 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 S. ------------------------ [email protected]
Hassan Schroeder | about.me
twitter: @hassan

sorry,
@potential_course_attendees should be @potential_course_students
Dani

On Sat, Dec 3, 2011 at 9:52 AM, Dani D. [email protected] 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 :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 S. ------------------------ [email protected]

twitter: @hassan

On Sat, Dec 3, 2011 at 9:27 AM, Dani D. [email protected] 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 S. ------------------------ [email protected]

twitter: @hassan

Hassan S. wrote in post #1034929:

On Sat, Dec 3, 2011 at 9:52 AM, Dani D. [email protected] 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 :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 S. ------------------------ [email protected]
Hassan Schroeder | about.me
twitter: @hassan

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.
regards
Dani

On Sat, Dec 3, 2011 at 10:19 AM, Dani D. [email protected] 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 :slight_smile:

HTH,

Hassan S. ------------------------ [email protected]

twitter: @hassan

Hassan S. wrote in post #1034937:

  1. 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

On Sat, Dec 3, 2011 at 11:13 AM, Dani D. [email protected] 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 :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,

Hassan S. ------------------------ [email protected]

twitter: @hassan