Saturday, August 19, 2006

rtags

I just submitted a patch for rtags.

rtags overwrites the existing TAGS file by default.
This patch adds an option "-a" or "--append"
so that rtags will append to the TAGS file instead of overwriting it.

This will allow rtags to index an entire directory (e.g. rails app)
find rails_app_dir -name '*.rb' -print -exec rtags -a {} \;

I just ran rtags on /usr/lib/ruby. Took a while to finish. Let's see how useful
it is going to be.

Using Capistrano for deployment

So I wanted to get started with capistrano and getting the rails app deployed to the main server (CentOS 4)
I ran into the lighttpd hang issue where the capistrano task to start the lighttpd process on the remote server hangs and doesn't return. The lighttpd process starts up fine though.
The following command solved the issue for me:

nohup sudo /usr/local/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf > /dev/null 2>&1

Another issue was starting backgroundrb processes on the remote servers. Capsitrano would invoke the command 'rake backgroundrb:start' and I could see the output, but doing a ps auxf on the remote server would show no backgroundrb process.
The following shell script works for me :

#!/bin/sh

echo "Starting the backgroundrb server"
echo "cd /var/www/xxx/current; rake backgroundrb:start"
cd /var/www/xxx/current
log="/tmp/backgroundrb.`date +%s`"
echo "Writing to $log"
nohup rake backgroundrb:start > $log 2>&1
cat $log
rm -f $log

Hopefully it will be useful to others.

Thursday, August 03, 2006

Hola

So I disappeared for a while to reappear as L6Clipper.
I will try to keep this blog focused on rails. If I am not posting to this blog, it is probably because l6clipper has taken over all my time.
But I have picked up a lot of new things in Rails. And the pace of new development in rails framework terrifies me. I spend way too much time on trying to do the right thing the first time around and within days, a newer better convention of solving the problem is out there on the web. Frustratingly cool! :)

Monday, April 24, 2006

Resuming Blogging

Resuming blogging after a brief hiatus.

- Have updated rails to 1.1.0.
- Frozen my rails application to Revision 4240 using a custom rake task. More details in next post

Wednesday, February 01, 2006

find, find_by_name and ActiveRecord::RecordNotFound

Given a User with 'has_many' messages:

message = user.messages.find do {|m| m.subject == messageSubject}

errors out with RecordNotFound exception if no such record exists; while

message = user.messages.find_by_subject(messageSubject)

doesn't error out with an exception, but it returns a nil object, which can then be tested for nil.

Passing an object to the block intialization autoamtically sets up association

If Obj A belongs_to Object B, then passing an object B in the block intialization of object B will automatically set up the database connections, i.e. user.note_id = note.id.

Consider a user.
class User < ActiveRecord::Base
....
belongs_to :note
end

A Note model
class Note < ActiveRecord::Base
end

The ddl is:
create_table users (
`id` int not null,
`note_id` int default NULL
)

create_table notes (
`id` int not null,
`note` text
)


Now consider the following code:

params[:user] contains intialization for the attributes of the user class.

params[:user][:note] = Note.new(:note => "ffo bar")
user = User.new(params[:user])

This will automatically insert the note into the database and then create the user record with note_id set to the id of this new note record.

Assigning to has_one or has_many association automatically saves object

Assigning to has_one or has_many association automatically saves object however assigning to a belongs_to association doesnt save the object.

A User has_many messages

controller:
u = User.find(session[:user_id])

messageSubject = params[:msgSub] || "No Subject"
message = u.messages.find_by_subject(messageSubject)
if message.nil?
u.messages << subject =""> messageSubject)
message = u.messages.last
end

development.log:
User Columns (0.000000) SHOW FIELDS FROM users
Message Columns (0.016000) SHOW FIELDS FROM messages
Message Load (0.000000) SELECT * FROM messages WHERE (messages.user_id = 1) AND (messages.`subject1 = 'No Subject' ) LIMIT 1
Couldn't find a message with name No Subject. Creating a new message.
Message Load (0.000000) SELECT * FROM messages WHERE (messages.user_id = 1) ORDER BY position
SQL (0.000000) BEGIN
Message Load (0.000000) SELECT * FROM messages WHERE (subject = 'No Subject') LIMIT 1
Message Load (0.000000) SELECT * FROM messages WHERE (user_id = 1) ORDER BY position DESC LIMIT 1
SQL (0.000000) INSERT INTO messages (`subject`, `updated_at`, `user_id`, `position`, `created_at`) VALUES('No Subject', '2006-02-01 12:41:48', 1, 1, '2006-02-01 12:41:48')
SQL (0.000000) COMMIT

Note that the reverse is not true...
If I do
pod = Pod.new(:name => podname)
pod.user = u

this will not save the pod until explictly saved!! Since Pod "belongs_to" a user, assigning to a "belongs_to" association doesn't automatically save either of the objects to the db.