# Newbie needing help! How to read from database?

**URL:** <https://www.ruby-forum.com/t/newbie-needing-help-how-to-read-from-database/57794>\
**Category:** Rails\
**Created:** [April 14, 2006, 7:04am UTC](https://www.ruby-forum.com/t/newbie-needing-help-how-to-read-from-database/57794 "2006-04-14T07:04:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ce73a5/32.png) [@tim](https://www.ruby-forum.com/u/tim)\
**Post date:** [April 14, 2006, 7:04am UTC](https://www.ruby-forum.com/t/newbie-needing-help-how-to-read-from-database/57794/1 "2006-04-14T07:04:49Z")

</div>

I’ve read through all the articles online but most/all of it discusses  
the use of Scaffolding as a quick solution for reading/writing to a  
database.

I follow all that but now I’m trying to simply get a field from a table  
in the database to display in my .rhtml files.

I’ve been trying to figure this out but to no avail. Someone please  
help! Thanks.

Tim

---

<div class="post-metadata">

**Author:** ![tim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ce73a5/32.png) [@tim](https://www.ruby-forum.com/u/tim)\
**Post date:** [April 14, 2006, 7:12am UTC](https://www.ruby-forum.com/t/newbie-needing-help-how-to-read-from-database/57794/2 "2006-04-14T07:12:16Z")

</div>

Sorry. To explain further, here’s what I tried to do:

This is the line of code in my index.rhtml:

\<%= link\_to\_remote(“Click Here”, :update =\> ‘introduce’, :url =\> {  
:action =\> :show\_intro }, :complete =\> visual\_effect(:blind\_down,  
‘introduce’) ) %\>

It’s a link to simply display a block of text taken from the database.

In my Controller:

def show\_intro  
@intro = Content.introText  
end

In my Model:

def self.introText  
find(1)  
end

Where 1 is the ID for the table row. There are 3 fields in my table: id,  
title and description.

I’m trying to get the text from the ‘description’ to display above.

---

<div class="post-metadata">

**Author:** ![tim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ce73a5/32.png) [@tim](https://www.ruby-forum.com/u/tim)\
**Post date:** [April 14, 2006, 8:16am UTC](https://www.ruby-forum.com/t/newbie-needing-help-how-to-read-from-database/57794/3 "2006-04-14T08:16:03Z")

</div>

So you should have a database table named “contents”.

Then in your controller:  
def show\_intro  
@intro = Content.find(1)  
end

In show\_intro.rhtml:  
\<%= @intro.description %\>

This assumes there is a column in your “contents” table called  
“description”. And you can leave your model “Content” completly virgin.  
You can test to see if it worked by just going to  
“[http://domain.com/my\_controller/show\_intro](http://domain.com/my_controller/show_intro)” and you should see your  
text from the database.

Although you may want to start out without the AJAX thing, until you  
have the basics down. It can make things much more complicated.

Alternatively if you still wanted “Content.intro\_text” (lowercase with  
underscores for method name is the standard ruby convention) to work:  
def Content \< ActiveRecord::Base  
def self.intro\_text  
find(1).description  
end  
end

Then in any .rhtml file:  
\<%= Content.intro\_text %\>

Remember that “Content.find(1)” returns an entire row as an ActiveRecord  
object. To access a specific field, just call it as a method to return  
its value:  
@content = Content.find(1)  
@content.description #=\> displays the description for row with id=1

Tim wrote:

> Sorry. To explain further, here’s what I tried to do:
> 
> This is the line of code in my index.rhtml:
> 
> \<%= link\_to\_remote(“Click Here”, :update =\> ‘introduce’, :url =\> {  
> :action =\> :show\_intro }, :complete =\> visual\_effect(:blind\_down,  
> ‘introduce’) ) %\>
> 
> It’s a link to simply display a block of text taken from the database.
> 
> In my Controller:
> 
> def show\_intro  
> @intro = Content.introText  
> end
> 
> In my Model:
> 
> def self.introText  
> find(1)  
> end
> 
> Where 1 is the ID for the table row. There are 3 fields in my table: id,  
> title and description.
> 
> I’m trying to get the text from the ‘description’ to display above.

---

<div class="post-metadata">

**Author:** ![tim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ce73a5/32.png) [@tim](https://www.ruby-forum.com/u/tim)\
**Post date:** [April 14, 2006, 10:08am UTC](https://www.ruby-forum.com/t/newbie-needing-help-how-to-read-from-database/57794/4 "2006-04-14T10:08:39Z")

</div>

Tim wrote:

> Thanks Alex!
> 
> That was very helpful. It didn’t work at first but I took it from there  
> and figured it all out without sacrificing the use of the Ajax  
> blind\_down effect.
> 
> Thanks again. Much appreciated.
> 
> Tim

Another thing you can do is to generate scaffolding for your models and  
read the code it creates. It’ll have simple actions that do things like  
add a record, delete a record, list and show a record.

ruby script/generate scaffold modelname

Replace “modelname” with the name of your model.

Jeff

---

<div class="post-metadata">

**Author:** ![tim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ce73a5/32.png) [@tim](https://www.ruby-forum.com/u/tim)\
**Post date:** [April 14, 2006, 9:16am UTC](https://www.ruby-forum.com/t/newbie-needing-help-how-to-read-from-database/57794/5 "2006-04-14T09:16:46Z")

</div>

Thanks Alex!

That was very helpful. It didn’t work at first but I took it from there  
and figured it all out without sacrificing the use of the Ajax  
blind\_down effect.

Thanks again. Much appreciated.

Tim
