Stupid TypeError while checking array values

Not sure if I’m even close to the mark on this. Ruby noob here…RACKIN
M’BRAINS HERE!! :frowning:

I am trying to check values of an array against another value.
Unfortunately, I keep getting a TypeError that says:

can’t convert User into Integer

I am trying to check if a logged in user has access to a course id based
on ownership assignments.

I have 2 tables having M:M relationship and a join table:


  •          *
    
  • Courses *
  •          *
    


  •                *
    
  • Courses_Users *
  •                *
    


  •          *
    
  • Users *
  •          *
    

…and here is my action

def user_assigned_course(course_id, user_id)
allowed_users = Course.find_by_id(course_id).users
allowed_users.each do |user|
if allowed_users[user] == user_id
return true
end
end
end

…help…

Ooops: forgot to add “.id” after “allowed_users[user]”

…and here is my action

def user_assigned_course(course_id, user_id)
allowed_users = Course.find_by_id(course_id).users
allowed_users.each do |user|
if allowed_users[user] == user_id
return true
end
end
end

…help…

def user_assigned_course(course_id, user_id)
allowed_users = Course.find_by_id(course_id).users
allowed_users.each do |user|
if allowed_users[user].id == user_id
return true
end
end
end

…yet this didn’t fix the error, so still:

HELP! :confused:

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

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

Rick A. wrote:

Ooops: forgot to add “.id” after “allowed_users[user]”

…and here is my action

def user_assigned_course(course_id, user_id)
allowed_users = Course.find_by_id(course_id).users
allowed_users.each do |user|
if allowed_users[user] == user_id
return true
end
end
end

…help…

def user_assigned_course(course_id, user_id)
allowed_users = Course.find_by_id(course_id).users
allowed_users.each do |user|
if allowed_users[user].id == user_id
return true
end
end
end

ooops blush

this worked:

def user_assigned_course(course_id, user_id)
allowed_users = Course.find_by_id(course_id).users
for user in allowed_users
if user.id == user_id
return true
end
end
end

Rick A. wrote:

def user_assigned_course(course_id, user_id)
allowed_users = Course.find_by_id(course_id).users
for user in allowed_users
if user.id == user_id
return true
end
end
end

And if you want to make your code a little bit cleaner, you can take
advantage of Ruby’s Enumerable#any? method:

def user_assigned_course(course_id, user_id)
Course.find_by_id(course_id).users.any? { |u| u.id == user_id }
end

Jamey

Confidentiality Notice: This email message, including any attachments,
is for the sole use of the intended recipient(s) and may contain
confidential and/or privileged information. If you are not the intended
recipient(s), you are hereby notified that any dissemination,
unauthorized review, use, disclosure or distribution of this email and
any materials contained in any attachments is prohibited. If you receive
this message in error, or are not the intended recipient(s), please
immediately notify the sender by email and destroy all copies of the
original message, including attachments.

On 10/9/06, Rick A. [email protected] wrote:

Wow, thanks guys! I better read up on making my Ruby code more
efficient…

You’re welcome. It really helps to browse the docs (e.g.
ruby-doc.orc/core) to see what’s in. Especially the Array, Hash,
String, Enumerable contain a lot of shortcuts.

J.

Wow, thanks guys! I better read up on making my Ruby code more
efficient…

Rick

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs