Ruby and ActiveRecord : Reading informations

A

aix aix

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
 
H

Hassan Schroeder

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??
 
K

Kendall Gifford

[Note: parts of this message were removed to make it a legal post.]

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
# 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top