1 def user_assigned_course(course_id, user_id)
2 allowed_users = Course.find_by_id(course_id).users
3 allowed_users.each do |user|
4 if allowed_users[user].id == user_id
5 return true
6 end
7 end
8 end
change line 4 to:
if user.id == user_id
user is not an index, it is an actual value.
BTW: you could also:
1. write return true before if (replace lines 4-5 with):
return true if user.id == user_id
2. use array function any? (replace lines 3-7 with):
return true if allowed_users.any? {|user| user.id == user_id }
probably you can even omit the 'return true if' part, so the whole
func could collapse to
def user_assigned_course(course_id, user_id)
Course.find_by_id(course_id).users.any? {|user| user.id == user_id }
end
although this might be too tight, as the left out 'allowed_users' adds
meaning to the .users array.