Rails US State Select

select(:user, :state, [
['Select a State', 'None'],
['Alabama', 'AL'],
['Alaska', 'AK'],
['Arizona', 'AZ'],
['Arkansas', 'AR'],
['California', 'CA'],
['Colorado', 'CO'],
['Connecticut', 'CT'],
['Delaware', 'DE'],
['District Of Columbia', 'DC'],
['Florida', 'FL'],
['Georgia', 'GA'],
['Hawaii', 'HI'],
['Idaho', 'ID'],
['Illinois', 'IL'],
['Indiana', 'IN'],
['Iowa', 'IA'],
['Kansas', 'KS'],
['Kentucky', 'KY'],
['Louisiana', 'LA'],
['Maine', 'ME'],
['Maryland', 'MD'],
['Massachusetts', 'MA'],
['Michigan', 'MI'],
['Minnesota', 'MN'],
['Mississippi', 'MS'],
['Missouri', 'MO'],
['Montana', 'MT'],
['Nebraska', 'NE'],
['Nevada', 'NV'],
['New Hampshire', 'NH'],
['New Jersey', 'NJ'],
['New Mexico', 'NM'],
['New York', 'NY'],
['North Carolina', 'NC'],
['North Dakota', 'ND'],
['Ohio', 'OH'],
['Oklahoma', 'OK'],
['Oregon', 'OR'],
['Pennsylvania', 'PA'],
['Rhode Island', 'RI'],
['South Carolina', 'SC'],
['South Dakota', 'SD'],
['Tennessee', 'TN'],
['Texas', 'TX'],
['Utah', 'UT'],
['Vermont', 'VT'],
['Virginia', 'VA'],
['Washington', 'WA'],
['West Virginia', 'WV'],
['Wisconsin', 'WI'],
['Wyoming', 'WY']])
Posted on October 30, 2008 at 10:21 pm

Preliminary Data with Migrations in Rails

At one time or another, you will most likely want your application to set-up with some preliminary data. One common example is a settings or options table within your database, or a default admin user that will be created upon install.

To get this done, is actually a lot easier then you may think..

Simply create a new migration such as:

script/generate migration add_default_user

Now all you need to do is add the data into the migration.

class AddDefaultUser < ActiveRecord::Migration
  def self.up
      User.create(
        :username => 'default', 
        :password => 'password'
       )
  end
 
  def self.down
  end
end

Once you have your migrations the way you like just run

rake db:migrate

to finish the job.

One thing to consider is deleting any records you have added to this migration in the self.down method, depending on your applications requirements. However, I think that this may also be accomplished when the (user) migration is removed.

Posted on September 28, 2008 at 8:20 pm

Put Sessions into Database with Rails

Adding sessions into the database is a great way to speed up your application, and it’s very easy to do in Rails 2.x. It can be done in the following, 3 easy, steps:

rake db:sessions:create

rake db:migrate

Now go into config/environment.rb and uncomment the following line:

# Use the database for sessions instead of the cookie-based default,
# which shouldn't be used to store highly confidential information
# (create the session table with 'rake db:sessions:create')
config.action_controller.session_store = :active_record_store

It’s is easy as that, now all of your sessions will be added to the database instead of files accessed at system level.

Cheers

Posted on August 30, 2008 at 4:33 pm

Benchmarking RadiantCMS on ModRails

After initially testing ModRails with my supreme hosting partner, I decided to try out a common application on the system. I still amazed at how fast this system is.. see for yourself:

zscott@Lian:~$ ab -n 1000 http://zak.railsplayground.net/
This is ApacheBench, Version 2.0.41-dev &lt;$Revision: 1.141 $&gt; apache-2.0
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 1998-2002 The Apache Software Foundation, http://www.apache.org/
 
Benchmarking zak.railsplayground.net (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Finished 1000 requests
 
 
Server Software:        Apache/2.0.59
Server Hostname:        zak.railsplayground.net
Server Port:            80
 
Document Path:          /
Document Length:        47 bytes
 
Concurrency Level:      1
Time taken for tests:   176.54368 seconds
Complete requests:      1000
Failed requests:        2
(Connect: 0, Length: 2, Exceptions: 0)
Write errors:           0
Total transferred:      481156 bytes
HTML transferred:       85390 bytes
Requests per second:    5.68 [#/sec] (mean)
Time per request:       176.054 [ms] (mean)
Time per request:       176.054 [ms] (mean, across all concurrent requests)
Transfer rate:          2.66 [Kbytes/sec] received
 
Connection Times (ms)
min  mean[+/-sd] median   max
Connect:       64   82  16.9     78     191
Processing:    65   92  72.8     80    1251
Waiting:       64   90  59.9     79    1164
Total:        135  175  75.4    161    1324
 
Percentage of the requests served within a certain time (ms)
50%    161
66%    170
75%    180
80%    186
90%    206
95%    236
98%    290
99%    401
100%   1324 (longest request)
Posted on April 16, 2008 at 7:42 pm

Benchmarking ModRails

Today Joe set-up beta testing for modrails on railsplayground, and a test account for me to play around with.

I quickly setup a basic Contact manager using Rails 2.02 scaffolding and added some test data. After the initial bump of forgetting to configure for production we were under way.

These are the results of benchmarking the application under 1,000 requests, looks good so far.

zscott@Lian:~$ ab -n 1000 http://zak.railsplayground.net/contacts
This is ApacheBench, Version 2.0.41-dev &lt;$Revision: 1.141 $&gt; apache-2.0
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 1998-2002 The Apache Software Foundation, http://www.apache.org/
 
Benchmarking zak.railsplayground.net (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Finished 1000 requests
 
Server Software:        Apache/2.0.59
Server Hostname:        zak.railsplayground.net
Server Port:            80
 
Document Path:          /contacts
Document Length:        2357 bytes
 
Concurrency Level:      1
Time taken for tests:   151.627619 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      3060000 bytes
HTML transferred:       2357000 bytes
Requests per second:    6.60 [#/sec] (mean)
Time per request:       151.628 [ms] (mean)
Time per request:       151.628 [ms] (mean, across all concurrent requests)
Transfer rate:          19.71 [Kbytes/sec] received
 
Connection Times (ms)
min  mean[+/-sd] median   max
Connect:       55   69  13.8     66     288
Processing:    60   81  36.4     75     739
Waiting:       59   77  29.2     71     586
Total:        118  151  39.2    144     808
 
Percentage of the requests served within a certain time (ms)
50%    144
66%    151
75%    156
80%    160
90%    174
95%    191
98%    222
99%    264
100%    808 (longest request)
Posted on April 13, 2008 at 1:23 pm

Exception Model Saving

Although there are many way’s to test whether or not your model has saved, this is a fool-proof way that I’ve come to love.

begin
  object.save!
rescue Record Invalid => error
  # failed
end
Posted on April 11, 2008 at 9:39 pm