Ruby and ActiveRecord : Reading informations

Discussion in 'Ruby' started by aix aix, May 24, 2011.

  1. aix aix

    aix aix Guest

    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

    --
    Posted via http://www.ruby-forum.com/.
    aix aix, May 24, 2011
    #1
    1. Advertising

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

    --
    Hassan Schroeder ------------------------
    http://about.me/hassanschroeder
    twitter: @hassan
    Hassan Schroeder, May 24, 2011
    #2
    1. Advertising

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

    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
    Kendall Gifford, May 24, 2011
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lionel
    Replies:
    0
    Views:
    385
    Lionel
    Aug 23, 2004
  2. Lionel
    Replies:
    1
    Views:
    319
    Andrew Thompson
    Aug 23, 2004
  3. Olivier
    Replies:
    3
    Views:
    340
  4. durumdara
    Replies:
    0
    Views:
    205
    durumdara
    Aug 14, 2009
  5. Lionel
    Replies:
    1
    Views:
    107
    Sisyphus
    Aug 24, 2004
Loading...

Share This Page