Forum: Ruby on Rails Accessing another table in a different rhtml file

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1ede4ef3f0ffd41bd08ddba1ce202eb5?d=identicon&s=25 Roshini (Guest)
on 2007-04-02 06:33
(Received via mailing list)
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 %>
    <td><%=h table1.ITEM_ID %></td>
<% 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
Ddc50fb60103da92d9561ead42ffb320?d=identicon&s=25 burlight (Guest)
on 2007-04-02 13:01
(Received via mailing list)
Hi
I'm new to rails too.
but have you assign value to table2 in your controller? (@table2 =
something).

burlight
1ede4ef3f0ffd41bd08ddba1ce202eb5?d=identicon&s=25 Roshini (Guest)
on 2007-04-03 08:34
(Received via mailing list)
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"
40db9e75b3f5899258e3bdc0c9210154?d=identicon&s=25 Conrad Taylor (Guest)
on 2007-04-03 09:11
(Received via mailing list)
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
1ede4ef3f0ffd41bd08ddba1ce202eb5?d=identicon&s=25 Roshini (Guest)
on 2007-04-03 10:25
(Received via mailing list)
Okay....but how would I access the @cars in the UserController?
D7c511ce5025d37b8c6bd9134e0f2bd9?d=identicon&s=25 Thorsten (Guest)
on 2007-04-03 10:51
(Received via mailing list)
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.
This topic is locked and can not be replied to.