Archive for the ‘Rails’ Category
Database Use with Rails
I really like Ruby on Rails for personal application development. If I had more time, I would probably invest more of this time learning Rails to actually help small businesses develop applications. Anyhow, I have created an accounting program called “Books” that is really easy to use and I wanted to describe here how the database is used and created.
When you create your Rails application a ton of code is written for you. The key database file is called database.yml and it’s found in the project tree at /config/database.yml. Here’s my file.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | # Licensed by Cape Henry Technologies Inc. # Cape Henry Technologies Inc. licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. # SQLite version 3.x # gem install sqlite3-ruby (not necessary on OS X Leopard) development: adapter: mysql encoding: utf8 database: books_development pool: 5 username: root password: socket: /tmp/mysql.sock # adapter: sqlite3 # database: db/numbers_development # pool: 5 # timeout: 5000 # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: adapter: mysql encoding: utf8 database: books_development pool: 5 username: root password: socket: /tmp/mysql.sock production: adapter: mysql encoding: utf8 database: books_development pool: 5 username: root password: socket: /tmp/mysql.sock |
Notice the database name is “books_development” for all database schemas. I commit the project files to Github and tag versions. I then download a zip of the version I want and explode it into a directory called “books-2010″ and I then change the database.yml file database names to “books-2010″. This way when I run the application and actually use it, the production data is safely handled by this application source code I downloaded.
When I make the application public, one would download the version, and issue the following commands in the project root directory.
20 21 22 | rake db:create <enter> rake db:migrate <enter> rake db:seed <enter> |
The seed functionality is very cool in it’s own right. You can make up a file to preload reference data or in my case, initial accounts for your chart of accounts. Here’s a copy of seeds.rb and it’s found at /db/seeds.rb.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | # This file should contain all the record creation needed to seed the database with its default values. # The data can then be loaded with the rake db:seed (or created alongside the db with db:setup). # # Examples: # # cities = City.create([{ :name => 'Chicago' }, { :name => 'Copenhagen' }]) # Major.create(:name => 'Daley', :city => cities.first) general_ledger = GeneralLedger.create([ { :company_name => 'Acme Mousetraps Inc.', :address1 => '123 Pleasantville Road', :address2 => '', :city => 'Omaha', :state => 'Nebraska', :zip => '76499', :registered_agent => 'John Doe', :tax_id => '1234567890' } ]) accounts = Account.create([ { :acct_no => '100', :acct_name => 'Cash', :acct_type => 'A', :balance => '1000.00', :general_ledger_id => '1' }, { :acct_no => '102', :acct_name => 'Accounts Receivable', :acct_type => 'A', :balance => '0.00', :general_ledger_id => '1' }, { :acct_no => '109', :acct_name => 'Capital', :acct_type => 'A', :balance => '0.00', :general_ledger_id => '1' }, { :acct_no => '103', :acct_name => 'Supplies', :acct_type => 'A', :balance => '0.00', :general_ledger_id => '1' }, { :acct_no => '104', :acct_name => 'Equipment', :acct_type => 'A', :balance => '0.00', :general_ledger_id => '1' }, { :acct_no => '202', :acct_name => 'Accounts Payable', :acct_type => 'L', :balance => '0.00', :general_ledger_id => '1' }, { :acct_no => '202', :acct_name => 'Tax Payable', :acct_type => 'L', :balance => '0.00', :general_ledger_id => '1' }, { :acct_no => '300', :acct_name => 'Drawing', :acct_type => 'L', :balance => '0.00', :general_ledger_id => '1' }, { :acct_no => '500', :acct_name => 'Rent Expense', :acct_type => 'E', :balance => '0.00', :general_ledger_id => '1' }, { :acct_no => '501', :acct_name => 'Supplies Expense', :acct_type => 'E', :balance => '0.00', :general_ledger_id => '1' }, { :acct_no => '502', :acct_name => 'Books Expense', :acct_type => 'E', :balance => '0.00', :general_ledger_id => '1' }, { :acct_no => '503', :acct_name => 'Meals Expense', :acct_type => 'E', :balance => '0.00', :general_ledger_id => '1' }, { :acct_no => '504', :acct_name => 'Auto Expense', :acct_type => 'E', :balance => '0.00', :general_ledger_id => '1' }, { :acct_no => '505', :acct_name => 'Misc Expense', :acct_type => 'E', :balance => '0.00', :general_ledger_id => '1' }, { :acct_no => '506', :acct_name => 'Electric Expense', :acct_type => 'E', :balance => '0.00', :general_ledger_id => '1' }, { :acct_no => '507', :acct_name => 'Internet Expense', :acct_type => 'E', :balance => '0.00', :general_ledger_id => '1' }, { :acct_no => '508', :acct_name => 'Liability Insurance Expense', :acct_type => 'E', :balance => '0.00', :general_ledger_id => '1' }, { :acct_no => '509', :acct_name => 'Medical Insurance Expense', :acct_type => 'E', :balance => '0.00', :general_ledger_id => '1' } ]) |
The third rake command you issue, finds this files and pre-loads your database with this data.