# Stupid TypeError while checking array values

**URL:** <https://www.ruby-forum.com/t/stupid-typeerror-while-checking-array-values/71556>\
**Category:** Ruby\
**Created:** [October 9, 2006, 10:17pm UTC](https://www.ruby-forum.com/t/stupid-typeerror-while-checking-array-values/71556 "2006-10-09T22:17:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/34f0e0/32.png) [@kly](https://www.ruby-forum.com/u/kly)\
**Post date:** [October 9, 2006, 10:17pm UTC](https://www.ruby-forum.com/t/stupid-typeerror-while-checking-array-values/71556/1 "2006-10-09T22:17:24Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![kly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/34f0e0/32.png) [@kly](https://www.ruby-forum.com/u/kly)\
**Post date:** [October 9, 2006, 10:19pm UTC](https://www.ruby-forum.com/t/stupid-typeerror-while-checking-array-values/71556/2 "2006-10-09T22:19:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/34f0e0/32.png) [@kly](https://www.ruby-forum.com/u/kly)\
**Post date:** [October 9, 2006, 10:20pm UTC](https://www.ruby-forum.com/t/stupid-typeerror-while-checking-array-values/71556/3 "2006-10-09T22:20:38Z")

</div>

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

HELP! 😕

---

<div class="post-metadata">

**Author:** ![kly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/34f0e0/32.png) [@kly](https://www.ruby-forum.com/u/kly)\
**Post date:** [October 9, 2006, 10:42pm UTC](https://www.ruby-forum.com/t/stupid-typeerror-while-checking-array-values/71556/4 "2006-10-09T22:42:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/34f0e0/32.png) [@kly](https://www.ruby-forum.com/u/kly)\
**Post date:** [October 9, 2006, 10:30pm UTC](https://www.ruby-forum.com/t/stupid-typeerror-while-checking-array-values/71556/5 "2006-10-09T22:30:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/34f0e0/32.png) [@kly](https://www.ruby-forum.com/u/kly)\
**Post date:** [October 9, 2006, 10:58pm UTC](https://www.ruby-forum.com/t/stupid-typeerror-while-checking-array-values/71556/6 "2006-10-09T22:58:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/34f0e0/32.png) [@kly](https://www.ruby-forum.com/u/kly)\
**Post date:** [October 9, 2006, 11:26pm UTC](https://www.ruby-forum.com/t/stupid-typeerror-while-checking-array-values/71556/7 "2006-10-09T23:26:59Z")

</div>

On 10/9/06, Rick A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![kly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/34f0e0/32.png) [@kly](https://www.ruby-forum.com/u/kly)\
**Post date:** [October 9, 2006, 11:19pm UTC](https://www.ruby-forum.com/t/stupid-typeerror-while-checking-array-values/71556/8 "2006-10-09T23:19:26Z")

</div>

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

Rick
