“Five quick steps to set up RVM with rails 2 and rails3 “

Posted by Piyush Gupta on August 06, 2010

1 – Installing RVM

$ sudo gem install rvm  

$ rvm-install

Finally put this lines in your .bash_profile or .bashrc:

  if [[ -s $HOME/.rvm/scripts/rvm ]] ; then
          source $HOME/.rvm/scripts/rvm
  fi

Restart your terminal, Rvm should be working by now.

2 – Installing ruby inside your rvm

$ rvm install 1.8.7

Will install ruby 1.8.7

$ rvm install ruby-head

Will install ruby 1.9.2 or newer.

3 – Setting up rvm to use your specific ruby version

$ rvm use 1.8.7

To use just this time.

$ rvm use 1.8.7 --default

To use always this version.

If you are using any other ruby replace the “1.8.7” for your version.

4 – Installing rails 2 and rails 3 RC inside different gemsets

You can separate your rails versions inside gemsets:

Rails 2:

Create the gemset:

$ rvm gemset create rails2

Setting up rvm to use always this gemset:

$ rvm use 1.8.7@rails2 --default

Installing rails 2:

$ gem install rails

Rails 3:

Create the gemset:

$ rvm gemset create rails3

Setting up rvm to use always this gemset:

$rvm use 1.8.7@rails3 --default

Installing rails 3:

$ gem install rails --pre

5 – Switching between different rails versions

Switching to rails 2:

$ rvm use 1.8.7@rails2
$ rails -v
Rails 2.3.5

Switching to rails 3:

$ rvm use 1.8.7@rails3
$ rails -v
Rails 3.0.0.rc

“Learning Rails”

Posted by Piyush Gupta on July 19, 2010

We all use Agile web development , ruby for rails, rails in 4 days, ruby , ajax etc books to get a hang of ruby on rails also some more books like pragmatic etc .

However I thought of making a list of websites which help do so

So here is the bunchedin : http://www.bunchedin.com/vuIMCc

“Error in ruby relative_path_from call on windows”

Posted by Piyush Gupta on June 28, 2010

I was playing around with “SPREE COMMERCE”after a long term on my windows machine , before this have always used my linux box for Spree commerce .

However got stuck at the very first step after bootstrapping a fresh application .

Error :

After scratching head for long and loosing 20 hairs atleast drilled down to source of error and monkey patched the error .

Moreover after a little digging, it was evident that the ‘relative_path_from’ method in ‘/ruby/lib/ruby/1.8/pathname.rb’ file had the problem. The problem was also described as a ticket at http://redmine.ruby-lang.org/issues/show/1366 and the excerpt of the issue is shown below:

Monkey Patch :

I added the capitalize! keyword at the end of the two lines so that both the paths are in uppercase, so that later in the code ( ) shown below does not barf and give us the error we got shown in the first error of the post.

Overriding original :

“Sending email with Ruby on Rails 2.3.5 and gmail”

Posted by Piyush Gupta on June 23, 2010

If you need an easy way to setup email for your company or for a client it’s hard to not love Google Apps. (The ’standard’ ie free version is here).

It is very likely that at some point you will need to send out emails from your application. Don’t cry. Gmail can do this for you. Rails 2.3.2 makes it easier than ever with the addition of ‘enable_starttls_auto’. Put the following code in production.rb, development.rb or environment.rb and you ’should’ be able to send out emails:

If you are one of the lucky ones this will actually work. It didn’t work for me but since I still needed to get email working here’s what I did:

First be sure to activate the email account that you are using. To do that you only need to login. Google will walk you through entering a captcha etc.

You might get this error:
Net::SMTPAuthenticationError: 530 5.7.0 Must issue a STARTTLS command first.

If you do then check your Ruby version. Ruby 1.8.7 has this built in but Ruby 1.8.6 does not.

The next best thing is to get action_mailer_tls. Most of the links you will find in forum and blog posts are broken. The code is on github and neatly packaged as a gem: http://github.com/openrain/action_mailer_tls/tree/master. Follow the directions to install and configure the plugin there and you should be able to send email.

Here’s a couple of handy conversations for reference:

http://www.railsforum.com/viewtopic.php?id=28480

http://www.ruby-forum.com/topic/184137

“New Features in Rails 3″

Posted by Piyush Gupta on June 22, 2010

We’re all about following the newest technology in web development. Rails 3 is coming soon, and it promises to be a significant upgrade. With the 4th beta version pushed recently, and a release candidate right around the corner, I thought now would be a good time to go over my two favorite new features.
All New Router

The router has been completely re-written and borrows heavily from the Merb router, but has a more developed DSL.

Regular Routes

Regular routes are more concise, but I’m not sure how I feel about the “controller#action” syntax yet…

Named Routes

Very similar to regular routes, just with an extra :as variable at the end.

Restful Routes

I think restful routes get the biggest improvement. The new syntax is MUCH easier to read than in Rails 2.

ActiveRecord and ActiveRelation

This is probably my favorite addition to Rails. The new ActiveRecord syntax is very similar to Doctrine (which we use with symfony). Instead of passing options as a hash to your find() method, they’ve created a bunch of new methods you can chain together.

The best part is that any of the query methods just return a relation instead of running the sql query. That way, you can chain more query methods on the relation later. The actual query doesn’t run until you try to access the object(s) (such as with an iterator). This makes fragment caching a lot easier, because you can keep your query call in your action. Even if the cache is hit, it will only create a relation and not actually query the database. You can also force the query to be run at any time by calling all(), first(), or last() on the relation.

“Ruby On Rails environment on Ubuntu 10.04

Posted by Piyush Gupta on June 22, 2010

I think this is my first technical post in the blog, so today I faced some problems while building the Ruby on Rails environment on my machine. I just like sharing this knowledge with you.

First, in this post we will make the following:

1. Ruby 1.9.1
2. Rails 2.3.5
3. Thin server.
4. MySQL server.

1. Ruby 1.9.1
The following commands mainly download the Ruby source code and compile it and so on

Just you have to install the following libraries:

sudo apt-get install build-essential libssl-dev libreadline5-dev zlib1g-dev

move to /usr/local/src to download source code and compile it in this folder
cd /usr/local/src

Download the source code, and unpack it

sudo wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.1-p0.tar.gz
sudo tar xzf ruby-1.9.1-p0.tar.gz
cd ruby-1.9.1-p0

Now, you have to check the version of gcc if it is 4.4 some errors will occur

dpkg -l | grep gcc

if gcc 4.4 found then you have to edit the cont.c file to go on. Open it up, and change the lines 90, 270 and 317 from #elif to #else.

sudo gedit cont.c

then write the following commands in terminal

sudo ./configure –prefix=/usr/local/ruby-1.9.1
sudo make
sudo make install

After these steps you have finally installed Ruby 1.9.1, to check its version

cd /usr/local/ruby-1.9.1/bin
ruby -v
output like that: ruby 1.9.1p0 (2009-01-30 revision 21907) [i686-linux]

Now, the ruby commands such as ruby, rake, irb, … etc are accessed only from /usr/local/ruby-1.9.1/bin, and all the commands are found in /usr/local/bin so just make link for the commands in ruby-1.9.1/bin and put them in /usr/local/bin

sudo ln -s /usr/local/ruby-1.9.1/bin/ruby /usr/local/bin
sudo ln -s /usr/local/ruby-1.9.1/bin/rake /usr/local/bin
sudo ln -s /usr/local/ruby-1.9.1/bin/gem /usr/local/bin
sudo ln -s /usr/local/ruby-1.9.1/bin/irb /usr/local/bin
sudo ln -s /usr/local/ruby-1.9.1/bin/ri /usr/local/bin
sudo ln -s /usr/local/ruby-1.9.1/bin/rdoc /usr/local/bin

2. Rails 2.3.5
Now, Ruby 1.9.1 is installed successfully on your machine, just add this command to terminal to install the Rails 2.3.5 gem

sudo gem install rails -v=2.3.5

3. Thin server
Also to install Thin application server gem

sudo gem install thin

4. MySQL server
Finally to install MySQL server for DB

sudo apt-get install mysql-server
and while installing it is required to set the root password

References: http://www.bunchedin.com/8Bolx6

Hope that it is some how useful for you

“Database Backup made easy : RAILS”

Posted by Piyush Gupta on June 09, 2010

From a long time we have been using “yaml_db” as a plugin for taking backups of DB .
Obviously, lots of people will say when a simple rake command and a sql command can do the same then why a plugin/gem , but when theres a bread already baked then concentrate on the curry why think about baking a new one .

Anyways , the following are some steps .
Source : http://www.railslodge.com/plugins/830-yaml-db

1. ruby script/plugin install git://github.com/adamwiggins/yaml_db.git

2. rake db:data:dump   ->   Dump contents of Rails database to db/data.yml
rake db:data:load   ->   Load contents of db/data.yml into the database


From the readme

= YamlDb

YamlDb is a database-independent format for dumping and restoring data.  It complements the the database-independent schema format found in db/schema.rb.  The data is saved into db/data.yml.

This can be used as a replacement for mysqldump or pg_dump, but only for the databases typically used by Rails apps.  Users, permissions, schemas, triggers, and other advanced database features are not supported – by design.

Any database that has an ActiveRecord adapter should work.

== Usage

rake db:data:dump   ->   Dump contents of Rails database to db/data.yml
rake db:data:load   ->   Load contents of db/data.yml into the database

Further, there are tasks db:dump and db:load which do the entire database (the equivalent of running db:schema:dump followed by db:data:load).

== Examples

One common use would be to switch your data from one database backend to another.  For example, let’s say you wanted to switch from SQLite to MySQL.  You might execute the following steps:

1. rake db:dump

2. Edit config/database.yml and change your adapter to mysql, set up database params

3. mysqladmin create [database name]

4. rake db:load

“Getting your meta tags on track : RAILS”

Posted by Piyush Gupta on June 05, 2010

Sick of the lame Rails puns in the headlines yet? Great!

Ok, so this post is a kind of ‘Ask the community’ post. How do you deal with meta tags including page titles? We should all know the ideal for titles is to be unique for each page on the site , but how do you go about this the rails way?

Here’s what I do (and it’s just one way of many) :

In my layout/application.html.erb

<title><%= @meta_title %> My Site Name</title>
<meta name="keywords" content="<%= @meta_keywords %>" />
<meta name="description" content="<%= @meta_description %>" />

In my application controller:

  before_filter :meta_defaults
  private

 def meta_defaults
    @meta_title = "Welcome to"
    @meta_keywords = "my keywords"
    @meta_description = "my meta description"
  end

and then in individual actions in my controllers I override the defaults

def view
    @article = Article.find(params[:id])
    @meta_title = "#{@article.name} - "
    @meta_description = @article.short_description
end

I’ve also seen suggestions for using yield and content_for, but to me that is a bit heavy-weight for simple strings.

So, what do you do?

“Rails ActiveRecord MySql varbinary “

Posted by Piyush Gupta on May 26, 2010

Although ActiveRecord has the following warning about database specific column types: “You may use a type not in this list as long as it is supported by your database (for example, “polygon” in MySQL), but this will not be database agnostic and should usually be avoided. ActiveRecord” You may have optimizations specific to a database server like MySql’s varbinary. Here is how to create the a table with a varbinary(1000) column.

create_table :page_error_sources do |t|
  t.column :source, 'varbinary(1000)', :null => false
  t.timestamps
end

Reference : MSQL VarBinary

“Ruby IRB Rails script/console tricks “

Posted by Piyush Gupta on May 26, 2010

I use script/console to play with any new features that I’m doing before they even make it into my app. I love using irb and script/console, but there were a few things that frustrated me.
1. Exit and reload the console when I make model changes
2. Command history was blown away on exit

So I decided to take some time to see if I could come up with solutions for the above. Luckily I found solutions for both of my problems.
Issue #1. After you make changes simply type “reload!” at the prompt to reload the app. This solved most of my problems, but still if I exit I lose all my previous commands.

Issue #2. Create the file ~/.irbrc to configure your irb settings. Include the following settings:

require 'irb/ext/save-history'
IRB.conf[:SAVE_HISTORY] = 200
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-history"

Voila, you now have a saved history just like a bash console. Use the “Up” and “Down” keys to browse your old history.

Technorati Tags: , , , ,