Its all about Ruby On Rails
tricks
Displaying information about your git repository
Nov 12th
Want to see information about your git repository?
Based on a discussion here Duane Johnson wrote a very useful bash script. I am using this script from months and would like to share with you. You can download this script here(git-info.txt (239)).
You can also add an alias like below, so that I can be a accessed by a single command “gitinfo”
alias gitinfo="/home/akhil/git-info.txt"
When you run this script from your working copy it displays:
- Remote URL
- Remote Branches
- Local Branches
- Configuration (.git/config)
- Most Recent Commit
Isn’t it useful, give it a try
Multiple versions of ruby on ubuntu
Jun 23rd
Three-Four days ago I was in a situation where I need to have multiple versions of ruby and rubygems on my ubuntu machine. I was lucky, I found an awesome article http://blog.michaelgreenly.com/2008/08/multiple-versions-of-ruby-on-ubuntu-2.html. This really solved my problem, Many thanks to Michael Greenly.
Printing large image diagrams generated by RailRoad
Mar 23rd
I use RailRoad for generating Ruby on Rails diagrams, but always wish I could print those diagrams. Generally diagrams are too big to print on a single A4 size paper and I didn’t find any tool to print larger images in parts so that I can join them. If I print the image generated by RailRoad on single page, it of no use as it is hardly readable.
Fortunately, two days ago I found something which resolved this issue. It is a linux command and print a particular image on four pages. Here is that command:
lp -o scaling=200 models.png
Hassle free installation of rails stack on debian based system
Feb 23rd
Want to install rails stack on a machine? Just follow these steps. It will setup a rails stack(Apache + passenger + mysql + ruby + rubygems + common gems + git) on any server(debian based)
- apt-get update
- apt-get upgrade -y
- apt-get -y install build-essential libssl-dev libreadline5-dev zlib1g-dev
- apt-get -y install mysql-server libmysqlclient15-dev mysql-client
- apt-get -y install ruby ruby1.8-dev irb ri rdoc libopenssl-ruby1.8
- install rubygems manually:
- download rubygems form rubyforge, >=1.3
- unzip files
- ruby setup.rb
- Check that gem command is in path. Sometimes ‘gem1.8′ is available but ‘gem’ not. In that case copy /usr/bin/gem1.8 to /usr/bin/gem using “cp /usr/bin/gem1.8 /usr/bin/gem”
- apt-get -y install libmagick9-dev
- apt-get -y install imagemagick
- apt-get -y install postfix mailx
- apt-get -y install apache2
- apt-get -y install apache2-prefork-dev
- wget http://webonrails.com/wp-content/plugins/download-monitor/download.php?id=7
- ruby install_gems.txt
- passenger-install-apache2-module
-
Download git from git-scm.com
- Unzip files
- ./configure –without-tcltk
- make -j 2
- make install
You are all set now, go deploy you rails app. I have tested it on linode(ubuntu8.10), slicehost(ubuntu 8.10), should work for you too
Edge rails: ActiveRecord::Base.each and ActiveRecord::Base.find_in_batches for batch processing
Feb 23rd
You guys must faced a situation when you need to process large number of records, for example sending newsletters. Then you must have done some sort of batch processing to save it eating up all of your memory. This feature is committed to rails core by DHH.
Now you can do something like User.each and User.find_in_batches.
Please refer here for more details.
rake -T does not list my custom rake task
Feb 13th
This is just a tip, may be you guys are already aware of it.
Sometimes I wonder why my custom rake task doesn’t appear in rake -T list, however it runs well. I just noticed that if I do not specify description for a task then that particular rake task does not appear in the task list.
Tip: Open DB shell/console from rails root dir
Jan 29th
Just a quick tip you might be using this already.
If you guys want to open your app’s DB shell. Then you can use rails utility ‘dbconsole’ by issuing “script/dbconsole” from rails root directory.
It will ask for DB password, and open your db shell.
If you use sake, you may like following sake task:
desc 'Launches the database shell using the values defined in config/database.yml'
task 'db:shell', :needs => [ 'environment' ] do
config = ActiveRecord::Base.configurations[(RAILS_ENV or "development")]
command = ""
case config["adapter"]
when "mysql" then
(command << "mysql ")
(command << "--host=#{(config["host"] or "localhost")} ")
(command << "--port=#{(config["port"] or 3306)} ")
(command << "--user=#{(config["username"] or "root")} ")
(command << "--password=#{(config["password"] or "")} ")
(command << config["database"])
when "postgresql" then
puts("You should consider switching to MySQL or get off your butt and submit a patch")
else
(command << "echo Unsupported database adapter: #{config["adapter"]}")
end
system(command)
end
ActionMailer Error: hostname was not match with the server certificate
Jan 28th
You guys may have stuck with following error while using ActionMailer with Rails 2.2.2 .
OpenSSL::SSL::SSLError (hostname was not match with the server certificate):
/usr/lib/ruby/1.8/openssl/ssl.rb:123:in `post_connection_check'
/usr/lib/ruby/1.8/net/smtp.rb:582:in `tlsconnect'
/usr/lib/ruby/1.8/net/smtp.rb:562:in `do_start'
/usr/lib/ruby/1.8/net/smtp.rb:525:in `start'
/vendor/rails/actionmailer/lib/action_mailer/base.rb:671:in `perform_delivery_smtp'
/vendor/rails/actionmailer/lib/action_mailer/base.rb:526:in `__send__'
/vendor/rails/actionmailer/lib/action_mailer/base.rb:526:in `deliver!'
/vendor/rails/actionmailer/lib/action_mailer/base.rb:392:in `method_missing'
/app/controllers/users_controller.rb:40:in `send_email_to_confirm_user'
If you use postfix, then you fix it quickly by disabling tls by setting “smtpd_use_tls=no” in /etc/postfix/main.cf .
Remember to restart postfix and rails app server.
Testing ActiveScaffold based Controllers
Dec 17th
If we are using ActiveScaffold in a controller, we might want to write test for that controller too. So we can write functional tests for all action that ActiveScaffold added to our controller like index, create, edit, update etc…
Since all these action are added by ActiveScaffold then these tests must go with ActiveScaffold plugin tests itself and I think they are at their place.
So we just need to test that ActiveScaffold is configured for that particular controller and uses the right model by:
assert_not_nil CampusController.active_scaffold_config assert CampusController.active_scaffold_config.model == Campus
Ofcourse, we also need to write tests for custom actions added by us.
Generating association while generating model
Dec 14th
May be you guys are already aware of, but in Rails 2.2 we can specify belongs_to association while generating model. So if we issue:
script/generate model Post title:string author:belongs_to
We will get a Post model like:
class Post < ActiveRecord::Base belongs_to :author end
Also it will automatically add the foreign key column in the migration.
class CreatePosts < ActiveRecord::Migration
def self.up
create_table :posts do |t|
t.string :title
t.belongs_to :author
t.timestamps
end
end
def self.down
drop_table :posts
end
end
Nothing much but saves some keystrokes.