Ruby and ActiveRecord : Reading informations

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

On Tue, May 24, 2011 at 2:30 AM, aix aix [email protected] 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??

On Tue, May 24, 2011 at 3:30 AM, aix aix [email protected] 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:

https://groups.google.com/forum/#!forum/rubyonrails-talk