Gene Horodecki
2006-Nov-04 02:11 UTC
[Rails] Basic Q: Please help with iterating through an AR.find result
I''m really stuck on this, and it seems so basic! It''s frustrating.. Thanks. I am working on a site that has Forums. There are two tables that track the users on this site. One table ''Forum'' has one entry per forum. The next table, ''ForumMember'' tracks each user''s status in each forum, if someone is a member of 10 forums, they have 10 ForumMember entries. Forum has_many ForumMembers, and ForumMember has_one Forum. I have a main page in which I list all the Forums that a user is a member of. I gather this data easily in my controller, with: def list @forums = Forum.find_by_user(@session[''user''].id) end However, I wish to extend this page and display the user''s status in the Forum on the corresponding line. The user''s status is in ForumMember. I don''t wish to do a SQL query for each forum on the list, so I want to prepare ahead of time by building a hash containing the corresponding ForumMember entries. I do this all the time in Perl.. I plan to index the hash to get the status as I iterate through the forums in the view. So I''m trying to do something like this: def list @forums = Forum.find_by_user(@session[''user''].id) forummembers = ForumMember.find_by_userid(@session[''user''].id) forummembers.each {|fm| @forummember[fm.forumid] = fm} @userid = @session[''user''].id end But Ruby tells me the method ''each'' is missing! Can anyone tell me why? I thought the .find returned an array of objects, which should have the each method! Thanks! -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060903/a2fbc432/attachment.html