SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How To? mySql + Ruby - Rails

    Hey,

    How can I use mysql with ruby without using rails?

    I searched google but still haven't found a beginning-to-end tutorial. I need my hand held lol.

  2. #2
    SitePoint Evangelist
    Join Date
    Apr 2005
    Posts
    485
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cgtemp
    Hey,

    How can I use mysql with ruby without using rails?

    I searched google but still haven't found a beginning-to-end tutorial. I need my hand held lol.
    on what OS?

    while the steps will vary depending on the particular OS, you basically need to install ruby, install a mysql adapter (so ruby can communicate with mysql) and the install mysql.

    you still may have to tweak some config files, but w/o knowing more about your system, nobody can help you without giving information for a number of different platforms.

  3. #3
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I run linux, and that is all I know about it.

    I have the latest RUBY version and the latest mysql.

    This is what confuses me, ruby isn't like php in this case? You can't just use some predefined variables to talk to mySQL? (Or am I wrong in this case, even though it seems that way when i do PHP..?)

    What adapter do you prefer? or should i stick to rails when it comes to using ruby + mysql for a web app? (I basically want to code something like collegehumor.com in ruby, and don't worry I'm an experianced coder, just kinda new to ruby. I'll start small, lol, thats just my long term goal.)

    Thanks for your response.

  4. #4
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You don't need to use Rails if you don't want to but its definately worth using ActiveRecord on its own instead of working with low level connection code and SQL statements. Assuming you have installed activerecord (which you will if you've installed Rails using rubygems) and the appropriate MySQL Ruby adapter - lets say you had an address and user table, the address table is a legacy table and the user table is a new one you've created that follows activerecord naming conventions - you could just do something like this:

    PHP Code:
    require 'rubygems'
    require_gem 'activerecord'

    # establish the connection
    ActiveRecord::Base.establish_connection(
      :
    adapter  => "mysql",
      :
    host     => "localhost",
      :
    username => "myuser",
      :
    password => "mypass",
      :
    database => "somedatabase"
    )

    # create a model for our ActiveRecord-friendly "users" table so we can work with it
    class User ActiveRecord::Base
    end

    # oh no, we have a legacy "address" table called "tbl_addr" with a primary key column called "addr_id" - no probs
    class Address ActiveRecord::Base
      set_table_name 
    "tbl_addr"
      
    set_primary_key "addr_id"
    end

    # now lets create some records
    user User.new
    user.first_name 'Luke'
    user.surname 'Redpath'
    user.age 23
    user
    .save

    # or all in one go
    address Address.create(
      :
    number => 123,
      :
    street => 'Some Street'
      
    :city => 'London'
      
    :postcode => 'N1 123'


  5. #5
    SitePoint Evangelist
    Join Date
    Apr 2005
    Posts
    485
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cgtemp
    I run linux, and that is all I know about it.

    I have the latest RUBY version and the latest mysql.

    This is what confuses me, ruby isn't like php in this case? You can't just use some predefined variables to talk to mySQL? (Or am I wrong in this case, even though it seems that way when i do PHP..?)

    What adapter do you prefer? or should i stick to rails when it comes to using ruby + mysql for a web app? (I basically want to code something like collegehumor.com in ruby, and don't worry I'm an experianced coder, just kinda new to ruby. I'll start small, lol, thats just my long term goal.)

    Thanks for your response.
    if nothing else, get the book "agile web development with rails" to learn about rails and to pick up development process techniques from guys who are excellent at what they do.

    i am very excited about rails because it strictly defines what i need to do and the prototyping ability is extremely nice. you can prototype sites in very little time - and seeing is believing, especially if you plan on selling your app.

    who will get the business... the developer who has a limited working model the next day or the developer who comes back with a limited working model in a week? who can charge more per hour?

    i use postgresql, so i'm not up to speed on how to install mysql. i'm set up on simply mepis (debian based). not all linux distros include the same ruby related packages - and debian is notorious for not including everything.

    this book will teach you what you need, though:

    http://www.amazon.com/gp/product/097...books&v=glance

    i may have some information for you at home, though. if i remember, i'll get it and post it here.

    you can always visit a ruby irc channel. open up your irc app and then search for a ruby irc channel.

    good luck.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •