I'm using the cancan gem, and in ability.rb, I wanted to check if a user with a certain role can update another object if and only if the user 'is part of' the other object (that is, there is an association between the models, and the user is found in the object's users method).
I wanted to use the include? method to see if @current_user is found in the resulting User array.
It always returns false when testing the code through ability.rb. If I run the same code in the rails console, it works fine.
I found out that include? does not work if the Array objects are not of the same class as the passed object, but I printed their class through class.name, and they are the same.
I ended up using detect and comparing id's, but I wanted to know if anyone has had this kind of issue before, and if you could shed some light on this.
Something similar happened with delete_if, where after deleting 20 out of a 100, count was still returning 100, but an each loop printed only the ones that were supposed to be there (20). This happened when executing within the RoR environment, but the rails console with the same code behaved as expected (array.count # => 20).
EDIT 20131007
A bit of code regarding the include? issue.
This bit returns false all the time, even though the user IS in the resulting array from course.institution.users.
can [:read, :update], Course do |course|
val = course.institution.users.include? user
end
If I take that same line, and try it for a given course and user in the rails console, like so:
Course.find(course_id).institution.users.include? User.find(user_id)
It works as it's supposed to, returning true then the user is found in the array and false if not.
So even, even if the == operator was weird in some way for this particular model, I'm dealing with the same arrays in both cases, so it should either bust or work well in both cases, not only within the cancan ability.rb... right?
==of your objects to ensure expected behavior ofArray#include?and other methods.