# How access to the tables whitout a model

**URL:** <https://www.ruby-forum.com/t/how-access-to-the-tables-whitout-a-model/58440>\
**Category:** Rails\
**Created:** [April 21, 2006, 6:13pm UTC](https://www.ruby-forum.com/t/how-access-to-the-tables-whitout-a-model/58440 "2006-04-21T18:13:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![X-1101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/bb73d2/32.png) [@X-1101](https://www.ruby-forum.com/u/X-1101)\
**Post date:** [April 21, 2006, 6:13pm UTC](https://www.ruby-forum.com/t/how-access-to-the-tables-whitout-a-model/58440/1 "2006-04-21T18:13:02Z")

</div>

i have a table called Products whit a model product, but in the view i  
need show the category od the product, but whitout a model of  
categories…

---

<div class="post-metadata">

**Author:** ![X-1101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/bb73d2/32.png) [@X-1101](https://www.ruby-forum.com/u/X-1101)\
**Post date:** [April 21, 2006, 6:33pm UTC](https://www.ruby-forum.com/t/how-access-to-the-tables-whitout-a-model/58440/2 "2006-04-21T18:33:12Z")

</div>

X-1101 wrote:

> i have a table called Products whit a model product, but in the view i  
> need show the category od the product, but whitout a model of  
> categories…

The simple answer is you don’t. You should have a model for each table  
of your database you plan on getting data from in your app (except for  
has\_and\_belongs\_to\_many join tables). Your model doesn’t have to have  
any code in it, it just has to be there. then you can

class Product \< ActiveRecord::Base  
belongs\_to :category  
end

Product.find(1).category.title

There is a way to do it without a model, but you would have to execute  
raw SQL with Product.find\_by\_sql(some\_sql) or a similar method. I’m not  
familiar with this so I can’t help you to much, but this seems like a  
far messier solution than simply creating a model.

---

<div class="post-metadata">

**Author:** ![X-1101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/bb73d2/32.png) [@X-1101](https://www.ruby-forum.com/u/X-1101)\
**Post date:** [April 21, 2006, 6:45pm UTC](https://www.ruby-forum.com/t/how-access-to-the-tables-whitout-a-model/58440/3 "2006-04-21T18:45:56Z")

</div>

How are your categories modelled? The rails way to do it would be to  
have a separate table for categories and a corresponding model. Then you  
could just add associations to the two models.

When you needed a list of categories (without products) you’d just do it  
through the model.

There is a way to use ActiveRecord’s connection to access the database  
directly through SQL and get a recordset. The real question is, why  
would you need to do something like that? Unless you have a very  
specific requirement that is unsupported by ActiveRecord, you should try  
and make use of the framework Rails provides to get it done more easily.  
After all, there is a reason why you don’t see many examples of direct  
database access in Rails code.

---

<div class="post-metadata">

**Author:** ![X-1101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/bb73d2/32.png) [@X-1101](https://www.ruby-forum.com/u/X-1101)\
**Post date:** [April 21, 2006, 6:59pm UTC](https://www.ruby-forum.com/t/how-access-to-the-tables-whitout-a-model/58440/4 "2006-04-21T18:59:00Z")

</div>

thanks Alex

i have a doubt, and i don understand

i have a table products

and i use the scaffold command “generate scaffold product admin”, and it  
generate the controller, models and views

i navigate in the browser and i can add, update, delete and list the  
products

my questions is

if i make a copy of the list.rhtml file and a call it like list2.rhtml

and i write in the browser “[http://localhost:3000/admin/lista](http://localhost:3000/admin/lista)”

apper the next error list

You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occured while evaluating nil.each

---

<div class="post-metadata">

**Author:** ![X-1101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/bb73d2/32.png) [@X-1101](https://www.ruby-forum.com/u/X-1101)\
**Post date:** [April 21, 2006, 7:04pm UTC](https://www.ruby-forum.com/t/how-access-to-the-tables-whitout-a-model/58440/5 "2006-04-21T19:04:33Z")

</div>

X-1101 wrote the following on 21.04.2006 18:13 :

> i have a table called Products whit a model product, but in the view i  
> need show the category od the product, but whitout a model of  
> categories…

You shouldn’t need to. If you have a categories table, simply create the  
Category model. In your controler, put the product you want to use in a  
@product instance variable and you can call @product.category.name (or  
whatever attribute you need) in your view. If you don’t have a category  
table but a category field with a “value” -\> “category\_name” association  
somewhere in your code (probably in a Hash), simply use this association  
to code a Product.category\_name method :

def category\_name  
CATEGORY\_NAMES[category]  
end

If you really want to do raw SQL I just had to do something not so  
different while coding a ‘VACUUM ANALYZE’ as part of a cleanup  
procedure. You can execute raw SQL using whatever existing model linked  
to a table on the same database by doing :  
Model.connection.execute(‘raw sql’).

But I repeat: you really don’t want to access your tables directly in  
your views, use ActiveRecord or put a method in the Product model…

Lionel.

---

<div class="post-metadata">

**Author:** ![X-1101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/bb73d2/32.png) [@X-1101](https://www.ruby-forum.com/u/X-1101)\
**Post date:** [April 21, 2006, 7:07pm UTC](https://www.ruby-forum.com/t/how-access-to-the-tables-whitout-a-model/58440/6 "2006-04-21T19:07:07Z")

</div>

X-1101 wrote:

> thanks Alex
> 
> i have a doubt, and i don understand
> 
> i have a table products
> 
> and i use the scaffold command “generate scaffold product admin”, and it  
> generate the controller, models and views
> 
> i navigate in the browser and i can add, update, delete and list the  
> products
> 
> my questions is
> 
> if i make a copy of the list.rhtml file and a call it like list2.rhtml
> 
> and i write in the browser “[http://localhost:3000/admin/lista](http://localhost:3000/admin/lista)”
> 
> apper the next error list
> 
> You have a nil object when you didn’t expect it!  
> You might have expected an instance of Array.  
> The error occured while evaluating nil.each

Thats because there is no controller action for “lista”. Rails renders  
“lista.rhtml” but does not fill the proper variable before hand. when  
go access “list” the list action of the controller gets invoked which  
primarily does

@products = Product.find(:all)

If the controller never executes the above line, then @products is nil.  
So when the view does:

\<% @products.each do |product| %\> #Same as “for product in @products”

You get an error since @products is nil, and nil does nto have an “each”  
method.

---

<div class="post-metadata">

**Author:** ![X-1101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/bb73d2/32.png) [@X-1101](https://www.ruby-forum.com/u/X-1101)\
**Post date:** [April 21, 2006, 7:38pm UTC](https://www.ruby-forum.com/t/how-access-to-the-tables-whitout-a-model/58440/7 "2006-04-21T19:38:36Z")

</div>

You can also add a :select=\>“category” to a find, and it will only fill  
in the ‘category’ attribute in the returned data. It shouldn’t be too  
hard to turn that into an array.

On Friday, April 21, 2006, at 7:01 PM, Lionel B. wrote:

> Category model. In your controler, put the product you want to use in a  
> If you really want to do raw SQL I just had to do something not so  
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

\_Kevin
