# Ruby and ActiveRecord : Reading informations

**URL:** <https://www.ruby-forum.com/t/ruby-and-activerecord-reading-informations/207339>\
**Category:** Ruby\
**Created:** [May 24, 2011, 11:29am UTC](https://www.ruby-forum.com/t/ruby-and-activerecord-reading-informations/207339 "2011-05-24T11:29:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [May 24, 2011, 11:29am UTC](https://www.ruby-forum.com/t/ruby-and-activerecord-reading-informations/207339/1 "2011-05-24T11:29:51Z")

</div>

Hello,

So now, before I use the DBI module to communicate with my MySQL  
database.

Now, I turn to ActiveRecord.

I’ll be home I connect, do queries, etc. However, recovery / reading  
information is unclear.

Here is my code:

require ‘rubygems’  
require ‘active\_record’

ActiveRecord::Base.establish\_connection(  
:adapter =\> ‘mysql’,  
:host =\> ‘localhost’,  
:username =\> ‘root’,  
:password =\> ‘toor’,  
:database =\> ‘demo’  
)

class Type\_service \< ActiveRecord::Base  
end

all = Type\_service.exists?()  
puts “table type\_services exist ? \n #{all}”

type = Type\_service.find(:all)  
puts “table elements : #{type}\n\n\n”

p type

And the result :

table type\_services exist ?  
true  
table elements:  
#\<Type\_service:0x7f0c5bb535d8\>#\<Type\_service:0x7f0c5bb53560\>

[#\<Type\_service id\_type\_s: 1, label\_type\_s: “D\303\251di\303\251”\>,  
#\<Type\_service id\_type\_s: 2, label\_type\_s: “Mutualis\303\251”\>]

How to get the results clearly?

Thanks

---

<div class="post-metadata">

**Author:** ![aix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a9a28c/32.png) [@aix](https://www.ruby-forum.com/u/aix)\
**Post date:** [May 24, 2011, 5:21pm UTC](https://www.ruby-forum.com/t/ruby-and-activerecord-reading-informations/207339/2 "2011-05-24T17:21:16Z")

</div>

On Tue, May 24, 2011 at 2:30 AM, aix aix [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> And the result :

> table elements:  
> #\<Type\_service:0x7f0c5bb535d8\>#\<Type\_service:0x7f0c5bb53560\>
> 
> [#\<Type\_service id\_type\_s: 1, label\_type\_s: “D\303\251di\303\251”\>,  
> #\<Type\_service id\_type\_s: 2, label\_type\_s: “Mutualis\303\251”\>]
> 
> How to get the results clearly?

def “clearly” – what do you want to see??

---

<div class="post-metadata">

**Author:** ![aix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a9a28c/32.png) [@aix](https://www.ruby-forum.com/u/aix)\
**Post date:** [May 24, 2011, 7:10pm UTC](https://www.ruby-forum.com/t/ruby-and-activerecord-reading-informations/207339/3 "2011-05-24T19:10:42Z")

</div>

On Tue, May 24, 2011 at 3:30 AM, aix aix [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> :database =\> ‘demo’  
> )
> 
> class Type\_service \< ActiveRecord::Base  
> end

# One possible example demonstrating data access…

puts “Dump of type\_services table:”  
puts “-” \* 80  
puts “id\_type\_s”.ljust(15, ’ ') + “| label\_type\_s”  
puts “-” \* 80  
Type\_service.find(:all).each do |record|  
puts record.id\_type\_s.to\_s.ljust(15, ’ ') + “| #{record.label\_type\_s}”  
end

> all = Type\_service.exists?()  
> puts “table type\_services exist ? \n #{all}”
> 
> type = Type\_service.find(:all)  
> puts “table elements : #{type}\n\n\n”
> 
> p type

The “type” local variable above references an entire collection of  
Type\_service instances. The collection can be used like any Enumerable  
object. Each individual entry (each Type\_service instance) corresponds  
to a  
record from your table. It will have an attribute accessor for each  
column  
in your table.

FYI: the convention when using ActiveRecord is to define your model  
class  
w/out the underscore and with each word capitalized:

class TypeService \< ActiveRecord::Base  
end

The above class would correspond to a table named “type\_services”.

Also, you should consider posting ActiveRecord questions in the ruby on  
rails mailing list since AR is part of rails:

> **[Redirecting to Google Groups](https://groups.google.com/forum/#!forum/rubyonrails-talk)**
