# How to refence to other table items

**URL:** <https://www.ruby-forum.com/t/how-to-refence-to-other-table-items/128763>\
**Category:** Rails\
**Created:** [February 11, 2008, 11:33am UTC](https://www.ruby-forum.com/t/how-to-refence-to-other-table-items/128763 "2008-02-11T11:33:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [February 11, 2008, 11:33am UTC](https://www.ruby-forum.com/t/how-to-refence-to-other-table-items/128763/1 "2008-02-11T11:33:01Z")

</div>

Hi

Since I am noob with ruby still I have question. This is my setup.

2 tables and their fields.

# Users table

login  
imitem\_id

# Imitems table

id  
groupname

Now all users have several Imitems and those are saved on user table  
with imitem\_id which points back to  
id on Imitems table.

Now on Imitem controller I have action:

def welcome

loginname = request.env[‘REMOTE\_USER’]

@users = User.find(:all, :conditions =\> [“login = ?”, loginname])

end

ie. it finds all users who’s loginname is the same as their  
REMOTE\_USER name

and on welcome.html.erb page it will print out all those entries which  
match that sql query.

\<% form\_for :user, @user, :url =\> { :action =\> “select” }, :html =\>  
{:id =\> ‘g  
rpform’, :name =\> ‘grpform’, :target =\> ‘\_parent’, :onsubmit =\>  
‘return validate\_task(this);’ } do |f| %\>  
\<% for user in @users %\>

\<%= h(user.imitem\_id) %\> \< %= link\_t o 'Destroy', user, :confirm =\> 'Are you sure you want to cancel this group sub scription ?', :method =\> :delete %\> [Edit] [MDM disable]

\<% end %\>  
\<% end %\>

Now however I need to print out also respective group name from Imitem  
table where loop

\<% for user in @users %\>

loops thru user items and then it should find out match for the items  
where

user.imitem\_id == imitem.id (and then return imitem.groupname)

ie. when user imitem.id number is equal to the imitem.id then I should  
find out what is that groupname on  
imitem table ?

Any ideas ?

---

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [February 11, 2008, 1:13pm UTC](https://www.ruby-forum.com/t/how-to-refence-to-other-table-items/128763/2 "2008-02-11T13:13:31Z")

</div>

first of all, there is no request.env[‘REMOTE\_USER’]. it just does not  
work that way. Have a look at acts\_as\_authenticated and install and use  
that.

---

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [February 11, 2008, 1:38pm UTC](https://www.ruby-forum.com/t/how-to-refence-to-other-table-items/128763/3 "2008-02-11T13:38:31Z")

</div>

Well there is and it works fine. But thats not the thing what I am  
asking here. You can think that loginname =  
request.env[‘REMOTE\_USER’]  
could be anything like loginname=jane

---

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [February 11, 2008, 2:04pm UTC](https://www.ruby-forum.com/t/how-to-refence-to-other-table-items/128763/4 "2008-02-11T14:04:01Z")

</div>

and I figured it out

\<% @imitems.each do |imitem| %\>  
\<% if imitem.id == user.imitem\_id %\>  
\<%= imitem.grpname %\>  
\<% end %\>  
\<% end %\>

also needed to add on models

user.rb

has\_many :imitems

imitem.rb

belongs\_to :user

---

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [February 11, 2008, 10:26pm UTC](https://www.ruby-forum.com/t/how-to-refence-to-other-table-items/128763/5 "2008-02-11T22:26:22Z")

</div>

I didn’t believe that and I even tested it myself. I put:

“puts request.env[‘REMOTE\_USER’]” in my blogs\_controller index action  
and  
checked it in Firefox on Windows XP

It returned nil.

I checked it on IE7 on WindowsXP

It returned nil.

I checked it on Firefox in Ubuntu.

It returned nil.
