Sunday, December 28, 2008

Current option not getting selected in select box?

I have a, options) in one of my form. But after I submit and if there are any errors in the form it should show the errors and retain all the values in the form. But this attr1 was loosing its value. Instead of the previously selected option, it being reset to the first option. What was wrong?

att_accessible! The attribute wanted to be selected automatically must be in the attr_accessible list. Otherwise Rails won't attach "selected" property to that option.

Thursday, November 20, 2008

Installing sqlite for rails development

  1. sudo apt-get install sqlite3
  2. sudo gem install sqlite3-ruby
  3. rails my_project
  4. rake db:create

Wednesday, November 19, 2008

Duplicating tables and problem with the structure

To duplicate a table:
mysql> CREATE TABLE products SELECT * FROM old_products;

But the problem with above statement is structure of products may not be the same as old_products. Attribute properties are not copied to the new table. For example of you have an auto_increment field named id, the auto_increment property won't be copied to products table. will be a normal integer field with the default value set to 0. It also means the new id field won't be the primary field. Took me 3 hours to figure this out.

So the best way to duplicate a table is:
1. CREATE TABLE products LIKE old_products;
2. INSERT INTO products SELECT * FROM old_products;

Thursday, October 30, 2008

Common MySQL stuff

To modify an existing column definition:


To rename a column:

ALTER TABLE applications CHANGE old_col new_col;

Wednesday, October 15, 2008

Sorting records based on the exact order of passed parameters to finder

In Ruby:
order = [5,2,3]
Project.find(:all, :conditions => ["id IN (?)", order], :order =>
"field(id, #{order.join(",")})")

SELECT * FROM projects WHERE (projects.`id` IN (1,2,3,5,7,6)) ORDER BY field(id, 5,1,2,3,7,6);

Wednesday, September 17, 2008

Basic Authentication using Apache and mod_rails

Copy pasted from the above link:
What steps will reproduce the problem?
1. Add a password file with htpasswd
ServerName caswell-box
DocumentRoot /home/user/app/public

Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
AuthName "Enter password"
AuthUserFile /etc/apache2/passfile.passwd
AuthType Basic
Require valid-user

3. Attempt to access the app in your web browser, and either enter the
wrong user/pass or hit cancel.

What is the expected output? What do you see instead?

The expected output is an authentication rejection, instead while the
static assets handled by Apache are restricted, the rails app is run by
passenger and generates pages without CSS/images.

What version of the product are you using? On what operating system?

Ubuntu 8.04, Passenger 1.9.1 RC2

Please provide any additional information below.

Looks like Passenger does not inherit the permissions from apache.

Monday, September 15, 2008

Find out mysql socket

  1. Enter mysql command line with 'mysql -u root -p'
  2. mysql> mysql status

Thursday, September 11, 2008

Duplicating a table in MySQL

CREATE TABLE duplicate_name SELECT * FROM original_table;

Thursday, August 28, 2008

GIT - Ignore uncommitted changes in already tracked files

in order to ignore uncommitted changes in already tracked files, please refer to the git update-index --assume-unchanged documentation

Thursday, July 31, 2008

TextMate discoveries

Option+Apple+o = Overwrite mode

Thursday, July 17, 2008

Wednesday, June 18, 2008

Migrating in a production environment

rake db:migrate RAILS_ENV=production
rake db:migrate RAILS_ENV="production"

Sunday, June 8, 2008

Add localhost.localdomain to local DNS (or hosts file)

  1. Goto Start > Run
  2. Type in "system32" (without the quotations)
  3. Goto the folder called "drivers"
  4. Goto the folder called "etc"

The hosts file should be in there. Just open it up with notepad or something and add in localhost.localdomain


Thursday, May 8, 2008

Padding numbers

If you want output as 123.90:

Tuesday, May 6, 2008 won't do what I intend to do, i.e. create a file. Instead,'w').close does the trick. The problem is in the first case the second argument defaults to 'r' which means read. Its strange that parameter is defaulted to read only. When we create file we normally mean we are creating it to write something to the file. So it must default to 'w'.

Saturday, May 3, 2008


I came across this weird sounding exception while creating a record. At first I didn't know what was wrong with my table. After a little probing I figured it was being caused by an attribute called 'frozen' in my table. I was using it to indicate if that record is frozen or not. But it appears Rails uses this keyword internally for the same purpose I was using that attribute for. I had to change this attribute to a different name (I named it 'frosen') for it to work.

This is what Rails api says about the error:
Raised when attribute has a name reserved by ActiveRecord (when attribute has name of one of ActiveRecord instance methods).

Monday, April 21, 2008

POSTing and PUTting from cURL

>curl -X PUT -d "post[title]=first post from curl" "http://localhost:3000/posts/1" -H "Accept: text/xml"

>curl -d "post[title]=first post from curl" "http://localhost:3000/posts" -H "Accept: text/xml"

-X POST is not needed in the second command because the default is POST when -d flag is used.

Thursday, April 17, 2008

Useful information in the request

Sometimes I have to extract some information from the incoming HTTP request. Most often I might need it for usage reports like browser or referring websites, etc. Here are some common bits I use:
Browser and OS => request.env['HTTP_USER_AGENT']
Referring website => request.env["HTTP_REFERER"]
IP => request.remote_ip
Session id => session.session_id

Wednesday, April 9, 2008

Behaviour of 'in' clause

I had this following code in one of my finder statements:
:conditions => ["subscription_id IN (?)",subscription_ids.join(",")]

That was producing the SQL statement:
SELECT * FROM memberships WHERE (subscription_id IN ('640,610,641,639,642,620,645'))

But that statement didn't yield any result set.

Whereas the following code:
:conditions => "subscription_id IN (#{subscription_ids.join(',')})"

..produces the SQL statement:
SELECT * FROM memberships WHERE (subscription_id IN (640,610,641,639,642,620,645))

which gave me the correct result set.

The difference between the 2 SQL statements is the usage of single-quotes around the numbers.

Monday, March 24, 2008

Generating unique random numbers

Interesting techniques:

Best one:
irb(main):001:0> (1..13).sort_by{rand}[0..7]
=> [7, 3, 8, 2, 11, 4, 1, 9]

Another one:

ar = []
while ar.length < 8
ar << rand(12) + 1

Friday, March 21, 2008

Ignoring files in SVN

To ignore schema.rb:
  1. Open command line and from rails root, cd db
  2. svn propset svn:ignore schema.rb .

What this means is, ignore schema.rb in the current folder. The dot at the end denotes you are ignoring something in the current directory.

Another example. To ignore all the files in the log directory:
  1. Open command line and cd to rails root
  2. svn propset svn:ignore log .
One last example. To ignore folder1 and folder2:
  1. Open command line and cd to rails root
  2. svn propset svn:ignore folder1 folder2 .

Here is a nice article that explains these in details.

Tuesday, March 18, 2008

alias_method for class methods

class Login
def self.super_user
system_admin = Role.find_by_desc("system_admin")
:conditions => "permissions.role_id = #{}",
:include => :permissions)

class << self
alias_method :system_admin, :super_user

Some links:

Loading fixtures separately

Recently I had to load one single fixture file in to the database. Here is how I did it:
>> messages = YAML.load( + '/test/fixtures/message_triggers.yml'))
>> messages.each{|m| MessageTrigger.create(m[1])}

Sunday, March 16, 2008

Date and Time Functions in MySQL

Quite often I am having to perform date and time calculations in my applications. Earlier I was doing all these things in Ruby. But later I realized these things will be faster and better if performed by the database engine. These calculations become cumbersome as they become complex. Here is a link to MySQL's date and time functions.

2 of the most commonly used functions:
  • Add an interval to a date and get back the updated date.
     DATE_ADD(s.expires_on, INTERVAL p.expiration_buffer DAY)
  • Return date in a particular format. In this case "2008-03-17"

Tuesday, March 11, 2008

Rails Gotchas: Routes

This won't work:
map.disclaimer '/disclaimer', :controller => :main, :action => :disclaimer

Whereas this works:
map.disclaimer '/disclaimer', :controller => "main", :action => "disclaimer"

The difference is: the first one contains symbols and the second strings.

Monday, March 10, 2008

Integer division

I was trying to divide 3 by 5 (3/5) and I never got the correct result. I got back 0 all the time which is obviously not correct. But when I did 6/2 I got back 3. So when I researched a little I found out that it defaults to integer division in Ruby. That means it doesn't divide the integer if the resultant is going to be a float. So I had to do this: 3.to_f/5.to_f.

An interesting discussion about this point:

Thursday, March 6, 2008

Column type mappings for databases

I always wondered how Rails maps the column types we define in migrations to appropriate data types in the database. There is no direct mapping between the data types that Ruby defines and the ones a database defines (not all at least). For example there is no string data type in any of the databases that I have worked with. I figured that today.

Here is a hash defined by mysql_adapter.rb:

def native_database_types #:nodoc:
:primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY KEY",
:string => { :name => "varchar", :limit => 255 },
:text => { :name => "text" },
:integer => { :name => "int", :limit => 11 },
:float => { :name => "float" },
:decimal => { :name => "decimal" },
:datetime => { :name => "datetime" },
:timestamp => { :name => "datetime" },
:time => { :name => "time" },
:date => { :name => "date" },
:binary => { :name => "blob" },
:boolean => { :name => "tinyint", :limit => 1 }

If you are using a different database then look up this hash in its respective adapter. Normally adapters are named like "#{database}_adapter.rb" under '\active_record\connection_adapters".

A detailed look at Ruby Vs MySQL mappings:

Tuesday, March 4, 2008

Instlling a specific version of a gem

gem install --version '0.4' hpricot
gem install --version '1.2.6' rails

and (&&) operand

I found the following block in routing.rb of rails framework.
routes.each do |route|
result = route.recognize(path, environment) and return result

I didn't understand this block at all. So I posted this in the Ruby mailing list. The good folks at the group answered me back immediately with some good examples. Here is the link:

To summarize:
If the first operand (we can even call it a statement), i.e. result = route.recognize(path, environment) retuns true then the following things would happen:
  1. 'result' variable contains the result of the recognize statement (which is a hash)
  2. The second operand (statement) gets evaluated which in turn ends the block by returning the result variable
If the first operand returns false then nothing happens so the loop continues.

But what is tricky in the statement "result = true and x = 20" is that 'result' contains true and not 20. Which means the first operand is also doing an assignment. I initially thought result will contain 20 because that's return value of the second statement.

Here is another explanation of the same subject:

Super stuff. Ruby Rocks.

Thursday, February 28, 2008

square brackets [] class method of Hash

[] class method of Hash converts an array into a hash.

irb(main):001:0> Hash["name","subbu","profession","programmer"]
=> {"name"=>"subbu", "profession"=>"programmer"}
irb(main):002:0> my_array = %w{ a b c d }
=> ["a", "b", "c", "d"]
irb(main):003:0> Hash[*my_array]
=> {"a"=>"b", "c"=>"d"}

Preventing sensitive data from being logged in Rails log file

filter_parameter_logging :password

2. Page 610 of 'Agile Web Development with Rails'

Route Globbing

The default route "map.connect ':controller/:action/:id'" is sufficient in most of the cases when dealing with URLs and routing. It matches most of the common URLs that are coming your way. But what if we need an URL like: which might mean:

There is something called as Route Globbing to solve this problem. In your routes.rb file specify the following line:
map.connect 'year/month/*values'

Routing engine bundles all the values after the third slash into params[:values] as an array. So your year/month action will have access to this array. The params[:value] will now look like ["5","28","18","30","20"].

If we want another controller to receive the URL, then we can say:
map.connect 'year/month/*values', :controller => "accounts"

Route globbing could be helpful if we are allowing users to enter some kind of organizational structure or file paths in to the URL.

Monday, February 25, 2008

String: extract a single character

Whenever I wanted to extract a single character from a string, invariably I started with something like "hello"[1] and Ruby returns 104. That's the ascii code for 'h'. Its not at all useful. I should practice saying "hello"[0,1] which will return what I want, i.e. 'h'.

Thursday, February 21, 2008

require and load won't reload the file with reload! in rails console

I have the statement "require 'primary_user' " in one of my models of my Rails application. Usually whenever I do a 'reload!' in my rails console, it reloads all the models and their associates files so that any changes to code can be seen in the console. But there is a catch to that reload!. It doesn't reload any of the files loaded using the 'require' statement. So my 'primary_user' file wasn't getting reloaded. Strange. But that's how it works. I tried changing 'require' to 'load' but no use.

Tuesday, February 19, 2008

Rails without a database

Add this to environment.rb:
config.frameworks -= [ :active_record]

This is what lines 19 to 21 in environment.rb say (Rails version 2.0.2):
  # Skip frameworks you're not going to use (only works if using vendor/rails).
# To use Rails without a database, you must remove the Active Record framework
# config.frameworks -= [ :active_record, :active_resource, :action_mailer ]

Sunday, February 17, 2008


eval("foo", binding)
Evaluate string in the context of binding. In this case 'foo' is executed as code in the context of binding. The second parameter is optional. It could either be a proc object or a binding object. If the second parameter is missing then the scope is set to current.

Evaluate the code inside the context of a class. Example:

Array.class_eval <<-addons
def total
self.inject do |s,e|


We can say something like "Array.class_eval('some goes here')" and the code will be executed within the context of Array class.

Same as class_eval

Here is an excellent article:

binding in ruby

This is what 'The Ruby Way' (Section 11.3.1) says about the 'binding':
You can encapsulate the current binding in an object using the method Kernel#binding. Having done that, you can pass the binding as the second parameter to eval, setting the execution context for the code being evaluated.

def some_ethod
a = "local variable"
return binding

the_binding = some_method
eval "a", the_binding # "local variable"

So, in an IRB session:

irb(main):001:0> a = "I am a local variable"
=> "I am a local variable"
irb(main):002:0> eval("a", binding)
=> "I am a local variable"

When we pass a block to the method, the binding preserves it.

def bindng
return binding

s = bindng{|v| puts v}
eval("yield('subbu')", s)

I don't know how this can be used though.

Another example that talks about binding with a nice context to use it:

Friday, February 15, 2008

ActiveRecord tricks - :include and :select options

When I say something like:

Project.find(:all, {
:select => ",,, tasks.owner",
:include => :tasks
The AR returns all fields from projects and tasks table. Whenever there is an :include option, it just ignores the :select options. To work around this, we can either specify the :join option like:

Project.find(:all, {
:select => ",,, tasks.owner",
:join => "LEFT OUTER JOIN tasks ON = WHERE = #{whatever}"

Or, we can specify the whole SQL like:
Project.find_by_sql("SELECT AS p_id, AS p_name, AS t_id, tasks.owner AS t_owner
FROM projects LEFT OUTER JOIN tasks ON = WHERE = #{whatever}")
We don't have to know a lot of SQL to write these work around solutions. Just look for the generated SQL query in the log file when you specified the :include option, copy that SQL, modify it and use it here.

Friday, February 8, 2008

Convert String to Range

class String
def to_range
case self.count('.')
when 2
elements = self.split('..')
return[0].to_i, elements[1].to_i)
when 3
elements = self.split('...')
return[0].to_i, elements[1].to_i-1)
raise"Couldn't convert to Range:

p "1..2".to_range
p "1...2".to_range
p "1.2".to_range

rng.test:11:in `to_range': undefined local variable or method `str'
for "1.2":String (NameError)
from rng.test:18

Thursday, February 7, 2008

Prevent a class from instantiating

class Myclass
class << self
undef_method :new

Wednesday, February 6, 2008

SQL joins explained

While trying to understand various 'sql joins' I stumbled upon some nice articles. The only problem with them was they were scattered across places. I had to put them in a word document so that I can print and take them home for further reading. Here are those documents.
A Visual Explanation of SQL Joins
Assorted articles on SQL Query Joins.pdf

Note: None of the material in these documents is written by me. Click on the link in "Original article at:" in the document to visit the original article.

Wednesday, January 16, 2008

What was Sun missing? DB => MySQL

Add methods to an instance

list = ["first item", "second item", "third item"]

# Add some useful methods to list

class << list
def total
def second
def third

>> list.second
# "second item"
# 3
>> list.third
# "third item"
>> list.first
# "first item" (first is a pre-defined Array method)

Select a random record from a DB table

In Rails:
Post.find(:first, :order => "rand()")


Monday, January 14, 2008

Removing instance variables

@new_user = User.find(:first)
@old_user = remove_instance_variable(:@new_user)
# @new_user is nil now
- Page 555 of pickaxe book under Object (or search for string remove_instance_variable)

Thursday, January 10, 2008

Bug Severity and Priority definitions

Severity - an objective, absolute, and technical assessment of the bug:
  1. Bug causes system crash and/or data loss.
  2. Bug causes major functionality or other severe problems; or product crashes in obscure cases.
  3. Bug causes minor functionality problems; or bug relates to wording issues in high visibility areas - impact is on "fit anf finish".
  4. Bug contains typos, unclear wording or error messages in low visibility areas.
Priority - a subjective, relative, and "business or consumer" assessment of the bug:
  1. Critical: Must fix NOW - bug is blocking further progress in testing.
  2. High: Must fix as soon as possible - prior to next build.
  3. Medium: Should fix soon, before product release.
  4. Low: fix if time; somewhat trivial. May be postponed.

Johanna Rothman provides another simplified way:
Instead of priority and severity, I use risk as a way to deal with problem reports, and how to know how to fix them. Here are the levels I choose:
  • Critical: We have to fix this before we release. We will lose substantial customers or money if we don’t.

  • Important: We’d like to fix this before we release. It might perturb some customers, but we don’t think they’ll throw out the product or move to our competitors. If we don’t fix it before we release, we either have to do something to that module or fix it in the next release.

  • Minor: We’d like to fix this before we throw out this product.

Here is the full article:

Tuesday, January 8, 2008

Struct, :answer)
=> Question"what's your first name?","subbu")
=> #
(behaves like an object)
=> "what's your first name?"
(behaves like a symbol)
=> "what's your first name?"
(behaves like a hash)
=> "what's your first name?"
(behaves like an array)
=> "what's your first name?"
This means, I can treat my Struct based on what the situation demands. Sometimes I can treat it like an array or a hash or an object or a symbol.

Create a project using earlier versions of Rails

rails _1.2.2_ myapplication

Just make sure 1.2.2(or whichever) version of rails gems including its dependencies are installed.

Wednesday, January 2, 2008

Undocumented Rails features - render collection with index

When I want to display the index of a collection in the following statement:
<%= render :partial => "subscription", :collection => @subscriptions %>

I can say:
<%= subscription_counter + 1 %>

The pattern is "partial_name_counter"

More here:

Blog Archive