From: Kendall Gifford Date: 2011-05-25T02:09:29+09:00 Subject: Re: Ruby and ActiveRecord : Reading informations --90e6ba6e845ce9ecbe04a408a53d Content-Type: text/plain; charset=ISO-8859-1 On Tue, May 24, 2011 at 3:30 AM, aix aix wrote: > 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 -- Kendall Gifford zettabyte@gmail.com --90e6ba6e845ce9ecbe04a408a53d--