Forum: Ruby on Rails How access to the tables whitout a model

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.
F48b1167605be400c501f6ceb808c533?d=identicon&s=25 X-1101 (Guest)
on 2006-04-21 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...
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (Guest)
on 2006-04-21 18:33
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.
Fb23bc8cd4030c526b0689276b34c8bd?d=identicon&s=25 Bryan Duxbury (bryanduxbury)
on 2006-04-21 18:45
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.
F48b1167605be400c501f6ceb808c533?d=identicon&s=25 X-1101 (Guest)
on 2006-04-21 18:59
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
Bef7ff8a0537495a1876ffebdc9f8e51?d=identicon&s=25 Lionel Bouton (Guest)
on 2006-04-21 19:04
(Received via mailing list)
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.
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (Guest)
on 2006-04-21 19:07
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.
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-04-21 19:38
(Received via mailing list)
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 Bouton 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
>Rails@lists.rubyonrails.org
>http://lists.rubyonrails.org/mailman/listinfo/rails


_Kevin
This topic is locked and can not be replied to.