# Accessing another table in a different rhtml file

**URL:** <https://www.ruby-forum.com/t/accessing-another-table-in-a-different-rhtml-file/90831>\
**Category:** Rails\
**Created:** [April 2, 2007, 6:33am UTC](https://www.ruby-forum.com/t/accessing-another-table-in-a-different-rhtml-file/90831 "2007-04-02T06:33:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roshini](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7c8e57/32.png) [@Roshini](https://www.ruby-forum.com/u/Roshini)\
**Post date:** [April 2, 2007, 6:33am UTC](https://www.ruby-forum.com/t/accessing-another-table-in-a-different-rhtml-file/90831/1 "2007-04-02T06:33:06Z")

</div>

Hi Everybody,

I am trying to access the elements of a different table within another  
tables layout file. Here is basically what I am trying to do:

\<% if table1.ITEM\_ID == table2.ID %\>

\<%=h table1.ITEM\_ID %\>  
\<% end %\>

But it gives me an error for table2. Here is the error I get:  
“undefined local variable or method”

I’m very new to RoR and would appreciate any help.

Thanks,  
Roshini

---

<div class="post-metadata">

**Author:** ![Roshini](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7c8e57/32.png) [@Roshini](https://www.ruby-forum.com/u/Roshini)\
**Post date:** [April 2, 2007, 1:01pm UTC](https://www.ruby-forum.com/t/accessing-another-table-in-a-different-rhtml-file/90831/2 "2007-04-02T13:01:57Z")

</div>

Hi  
I’m new to rails too.  
but have you assign value to table2 in your controller? (@table2 =  
something).

burlight

---

<div class="post-metadata">

**Author:** ![Roshini](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7c8e57/32.png) [@Roshini](https://www.ruby-forum.com/u/Roshini)\
**Post date:** [April 3, 2007, 8:34am UTC](https://www.ruby-forum.com/t/accessing-another-table-in-a-different-rhtml-file/90831/3 "2007-04-03T08:34:26Z")

</div>

Well for now, I have it in a for loop that goes through the entire  
table2 db to match and see if the same ID exists in both tables. But,  
everytime I make any reference to table2 even in the for loop, it  
throws the same error "“undefined local variable or method”

---

<div class="post-metadata">

**Author:** ![Roshini](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7c8e57/32.png) [@Roshini](https://www.ruby-forum.com/u/Roshini)\
**Post date:** [April 3, 2007, 10:25am UTC](https://www.ruby-forum.com/t/accessing-another-table-in-a-different-rhtml-file/90831/4 "2007-04-03T10:25:44Z")

</div>

Okay…but how would I access the @cars in the UserController?

---

<div class="post-metadata">

**Author:** ![Roshini](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7c8e57/32.png) [@Roshini](https://www.ruby-forum.com/u/Roshini)\
**Post date:** [April 3, 2007, 10:51am UTC](https://www.ruby-forum.com/t/accessing-another-table-in-a-different-rhtml-file/90831/5 "2007-04-03T10:51:58Z")

</div>

you have to poulate the @cars variable in the UserController, just  
like you did with @users:

def UserController \< ApplicationController

```
def list

    @cars = Car.list
    @users = User.list

end

```

end

simple as that. the above is only pseudo code obviously, as you did  
not give any code example what you do in your controller to find your  
desired dataset.

---

<div class="post-metadata">

**Author:** ![Roshini](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7c8e57/32.png) [@Roshini](https://www.ruby-forum.com/u/Roshini)\
**Post date:** [April 3, 2007, 9:11am UTC](https://www.ruby-forum.com/t/accessing-another-table-in-a-different-rhtml-file/90831/6 "2007-04-03T09:11:38Z")

</div>

Hi, you should be able to easily retrieve values from other tables.  
For example, if you have have the following:

Example models:

class User \< ActiveRecord::Base

def self.list

```
   User.find( :all )

```

end

end

class Car \< ActiveRecord::Base

```
def self.list

     Car.find( :all )

end

```

end

Example controllers:

def CarController \< ApplicationController

```
def list

    @cars = Car.list

end

```

end

class UserController \< ApplicationController

```
def list

   @users = User.list

end

```

end

Now. in your views, you can easily reference both the @users and @cars  
instance variables within your views. Please remember that each model  
represents an OO view of the underlying table (i.e. cars and users  
tables). Again, this is just an example and you can customize it to  
do whatever you like. For example, you might want to get a subset of  
the available users or retrieve a user by an attribute. If this is  
the case, then you can easily define the appropriate method(s) within  
the controller and/or the model to achieve this goal. Well, I hope  
that this helps and I wish you the best of luck.

-Conrad
