How access to the tables whitout a model

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…

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.

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.

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

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

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.

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

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.

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
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

_Kevin