# Conditions array results in undefined

**URL:** <https://www.ruby-forum.com/t/conditions-array-results-in-undefined/91039>\
**Category:** Rails\
**Created:** [April 3, 2007, 11:47pm UTC](https://www.ruby-forum.com/t/conditions-array-results-in-undefined/91039 "2007-04-03T23:47:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 3, 2007, 11:47pm UTC](https://www.ruby-forum.com/t/conditions-array-results-in-undefined/91039/1 "2007-04-03T23:47:21Z")

</div>

Hi!

I’m having another small problem. I run a small query to get all  
the group\_id’s that are associated with a user. I then take that and  
run it in another query in order to display the relevant data/group ID  
the user belongs in. But when I do this, sometimes it returns an  
error because there’s ‘nomethod’ in array, and other times it returns  
nothing. For example, this is my code…

def list  
username = ‘bob’  
resUser = User.find(:all, :conditions =\> [“username = :name”,  
{:name =\> username}], :select =\> “group\_id”)

```
@result_pages, @results = paginate :results, :per_page =>

```

10, :conditions =\> [“group\_id IN (?)”, resUser]  
end

In the logs, it does the first query, but for the second, it shows  
‘null’.  
SQL (0.000314) SELECT count(\*) AS count\_all FROM results WHERE  
(group\_id IN (NULL))

* * *

Now if I change some of the code to look like this…

```
username = 'bob'
resUser = User.find(:all, :conditions => ["username = :name",

```

{:name =\> username}])

```
@result_pages, @results = paginate :results, :per_page =>

```

10, :conditions =\> [“group\_id IN (?)”, resUser.group\_id]

It flashes an NoMethodError exception with…

undefined method `group\_id’ for #Array:0x352ca94

To my knowledge, there were some tickets opened on this but it looks  
like it has been fixed. One of them is here.

[http://dev.rubyonrails.org/ticket/6150](http://dev.rubyonrails.org/ticket/6150)  
[http://dev.rubyonrails.org/ticket/2887](http://dev.rubyonrails.org/ticket/2887)

But I’m still having problems trying to get this to work. If anybody  
can help, that would be great.

Thanks in advanced…

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 4, 2007, 12:46am UTC](https://www.ruby-forum.com/t/conditions-array-results-in-undefined/91039/2 "2007-04-04T00:46:33Z")

</div>

removed\_email\_address@domain.invalid wrote:

> def list  
> username = ‘bob’  
> resUser = User.find(:all, :conditions =\> [“username = :name”,  
> {:name =\> username}], :select =\> “group\_id”)
> 
> ```
> @result_pages, @results = paginate :results, :per_page =>
> 
> ```
> 
> 10, :conditions =\> [“group\_id IN (?)”, resUser]  
> end

In both cases you seem to be expecting a sinlge user, but you are asking  
for :all matching records instead of just one. Because of this you are  
getting an array of users, not just one user. So when you call a method  
like “group\_id” you call it on the array, not the user object inside of  
it.

Try this instead

res\_user = User.find(:first, :conditions =\> {:username =\> ‘bob’})

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 4, 2007, 4:14pm UTC](https://www.ruby-forum.com/t/conditions-array-results-in-undefined/91039/3 "2007-04-04T16:14:15Z")

</div>

That is true and that does work. But, users can belong to multiple  
groups. So, for example, the table ‘users’ can look like this…

Username - Group\_ID - Group  
Bob - 1 - HR  
Jim - 2 - OCF  
Tim - 1 - HR  
Mike - 3 - MED  
Bob - 3 - MED

In the results table, a ‘result’ would belong to a group\_id. So, if  
‘Bob’ logs in, he should be able to view MED and HR results, but not  
OCF. If I use the ‘first’ instead of ‘all’, then I only get the first  
instance, which is ‘HR’. I hope that makes sense.

Thank you again for any help!

On Apr 3, 5:46 pm, Alex W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
