Ruby on Rails, Active Record — Object-relation mapping put on rails

My Active Record Tests

start gem server and browse to the full documentation http://localhost:8808/doc_root/activerecord-2.0.2/rdoc/index.html Sample table used CREATE TABLE `test`.`users` ( `id` int(10) unsigned NOT NULL auto_increment, `name` varchar(45) NOT NULL, `age` int(11) default NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM; Table has one record id=1 name=”Sreeprakash”, age=51 Model used user.rb class User < ActiveRecord::Base end Controller used users_controller.rb class UsersController < ApplicationController # we will fill test functions to test the ActiveRecord methods end No Views used Browser URLhttp://localhost:3000/users/test

Simple example (1/2): Defining tables and classes (using MySQL)

Data definitions are specified only in the database. Active Record queries the database for the column names (that then serves to determine which attributes are valid) on regular object instantiation through the new constructor and relies on the column names in the rows with the finders.

   # CREATE TABLE companies (
   #   id int(11) unsigned NOT NULL auto_increment,
   #   client_of int(11),
   #   name varchar(255),
   #   type varchar(100),
   #   PRIMARY KEY  (id)
   # )

Active Record automatically links the “Company” object to the “companies” table

   class Company < ActiveRecord::Base
     has_many :people, :class_name => "Person"
   end

   class Firm < Company
     has_many :clients

     def people_with_all_clients
      clients.inject([]) { |people, client| people + client.people }
     end
   end

The foreign_key is only necessary because we didn’t use “firm_id” in the data definition

   class Client < Company
     belongs_to :firm, :foreign_key => "client_of"
   end

   # CREATE TABLE people (
   #   id int(11) unsigned NOT NULL auto_increment,
   #   name text,
   #   company_id text,
   #   PRIMARY KEY  (id)
   # )

Active Record will also automatically link the “Person” object to the “people” table

   class Person < ActiveRecord::Base
     belongs_to :company
   end

Simple example (2/2): Using the domain

Picking a database connection for all the Active Records

   ActiveRecord::Base.establish_connection(
     :adapter  => "mysql",
     :host     => "localhost",
     :username => "me",
     :password => "secret",
     :database => "activerecord"
   )

Create some fixtures

   firm = Firm.new("name" => "Next Angle")
   # SQL: INSERT INTO companies (name, type) VALUES("Next Angle", "Firm")
   firm.save

   client = Client.new("name" => "37signals", "client_of" => firm.id)
   # SQL: INSERT INTO companies (name, client_of, type) VALUES("37signals", 1, "Firm")
   client.save

Lots of different finders

   # SQL: SELECT * FROM companies WHERE id = 1
   next_angle = Company.find(1)

   # SQL: SELECT * FROM companies WHERE id = 1 AND type = 'Firm'
   next_angle = Firm.find(1)

   # SQL: SELECT * FROM companies WHERE id = 1 AND name = 'Next Angle'
   next_angle = Company.find(:first, :conditions => "name = 'Next Angle'")

   next_angle = Firm.find_by_sql("SELECT * FROM companies WHERE id = 1").first

The supertype, Company, will return subtype instances

   Firm === next_angle

All the dynamic methods added by the has_many macro

  next_angle.clients.empty?  # true
  next_angle.clients.size    # total number of clients
  all_clients = next_angle.clients

Constrained finds makes access security easier when ID comes from a web-app

   # SQL: SELECT * FROM companies WHERE client_of = 1 AND type = 'Client' AND id = 2
   thirty_seven_signals = next_angle.clients.find(2)

Bi-directional associations thanks to the “belongs_to” macro

   thirty_seven_signals.firm.nil? # true

43 Things Tags: , Active Record – Object-relation mapping put on rails 43 Things Tags: , Active Record – Object-relation mapping put on rails

How to install Ruby on Rails in Windows

Quick notes on how to install Ruby on Rails under Windows

  1. Open http://rubyforge.iasi.roedu.net/files/rubyinstaller/ruby186-26.exe or any later version if applicable
  2. Install to c:ruby
  3. Windows -> Start -> Run -> cmd to get the dos prompt
  4. Type PATH to ensure that it has c:rubybin; if not fix this manually via Windows control panel settings.
  5. Download http://rubyforge.rubyuser.de/rubygems/rubygems-1.1.1.zip
  6. Unzip to c:rubygems
  7. cd rubygems
  8. ruby setup.rb (this installs executable gem at c:rubybingem)
  9. delete rubygems folder
  10. gem update –system (this is to update rubygems – not needed here now)
  11. gem update (this is to update installed gems – not needed here now)
  12. gem install rails –include-dependencies
  13. MySQL – download mysql and install it, there are many ways (xampp etc)
  14. DownLoad MySQL Admin tool and connect to your MySQL
    http://mysql.orst.edu/Downloads/MySQLGUITools/mysql-gui-tools-5.0-r12-win32.msi
  15. Create a table called users
    id auto_inc
    name varchar 50
    age int 3
  16. Make a folder say c:ror
  17. cd ror
  18. rails my_first_app -d mysql
  19. cd my_first_app
  20. ruby scriptserver
  21. start browser and point to http://localhost:3000
  22. Scaffolding a table
  23. ruby scriptgenerate scaffold user name:string age:integer
  24. Edit configdatabase.yml
  25. ruby scriptserver
  26. start browser and point to http://localhost:3000/users