Verifying the IBM_DB Rails adapter installation
To verify that the IBM_DB Rails adapter is installed correctly, build and run a sample Rails application.
Procedure
- Create a new Rails application by issuing the following
command:
C:\>rails new newapp --database=ibm_db create create app/controllers create app/helpers create app/models create app/views/layouts create config/environments create config/initializers create db [......] create log/server.log create log/production.log create log/development.log create log/test.log
- Change the current directory to the newly created newapp directory:
C:\>cd newapp
- Optional: If you are using a Rails version
before the Rails 2.0, you must register the IBM_DB adapter to the
list of connection adapters in the Rails framework. You can register
the IBM_DB adapter to the list of connection adapters in the Rails
framework by manually adding
ibm_db
to the list of connection adapters in <RubyHome>\gems\1.8\gems\activerecord-1.15.6\lib\active_record.rb at approximately line 77:RAILS_CONNECTION_ADAPTERS = %w( mysql postgresql sqlite firebird sqlserver db2 oracle sybase openbase frontbase ibm_db )
- To configure the connections for the Rails
applications, edit the database.yml file. A sample
development section entry for the database.yml file
is listed in the following example:
development: adapter: ibm_db username: db2inst1 password: secret database: devdb # Database name #schema: db2inst1 #host: localhost #Host on which the database resides #port: 50000 #port to which the remote Dataserver is listening
- Create a model and a scaffold by issuing the rails command:
C:\>rails generate scaffold Tool name:string model_num:integer exists app/models/ exists app/controllers/ [….] create db/migrate create db/migrate/20080716103959_create_tools.rb
- Create the tools table in the devdb database
by issuing the rake db:migrate command:
C:\ >rake db:migrate (in C:/ruby trials/newapp) == 20080716111617 CreateTools: migrating ====================================== -- create_table(:tools) -> 0.5320s == 20080716111617 CreateTools: migrated (0.5320s)
The Rails application can now access the tools table.
- To test the application, issue the rails console command:
C:\ruby trials\newapp>rails console Loading development environment (Rails ) >> tool = Tool.new => #<Tool id: nil, name: nil, model_num: nil, created_at: nil, updated_at: nil> >> tool.name = 'chistel' => "chistel" >> tool.model_num = '007' => "007" >> tool.save => true >> Tool.find :all => [#<Tool id: 100, name: "chistel", model_num: 7, created_at: "2008-07-16 11:29:35", updated_at: "2008-07-16 11:29:35">] >>