Ruby on Rails Archives

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.

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

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

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

Simple example (2/2): Using the domain

Picking a database connection for all the Active Records

Create some fixtures

Lots of different finders

The supertype, Company, will return subtype instances

All the dynamic methods added by the has_many macro

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

Bi-directional associations thanks to the “belongs_to” macro

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