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