Stupid TypeError while checking array values

R

Rick Armstrong

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

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

Rick Armstrong

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
 
R

Rick Armstrong

Rick said:
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
 
J

Jan Svitok

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

Jamey Cribbs

Rick said:
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.
 
R

Rick Armstrong

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

Rick
 
J

Jan Svitok

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,214
Messages
2,571,112
Members
47,705
Latest member
noname22

Latest Threads

Top