Forum: Ruby on Rails conditions array results in undefined

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
5cfe2c740d00a1b243e93ed79fdf3a1b?d=identicon&s=25 (Guest)
on 2007-04-03 23:47
(Received via mailing list)

   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]

In the logs, it does the first query, but for the second, it shows
  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.

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

Thanks in advanced...
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (squeegy)
on 2007-04-04 00:46 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

Try this instead

  res_user = User.find(:first, :conditions => {:username => 'bob'})
5cfe2c740d00a1b243e93ed79fdf3a1b?d=identicon&s=25 (Guest)
on 2007-04-04 16:14
(Received via mailing list)
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 Wayne <>
This topic is locked and can not be replied to.