Saturday, February 26, 2011

Getting error "uninitialized constant Rake::RDocTask" while running rake db:migrate

I am upgrading an existing rails application from Rails verison 2.3.5 to 3.0.3.
I added some files, and while running rake db:migrate, getting error:
uninitialized constant Rake::RDocTask
After long searching, running rake --trace and analyzing found the fix.
Added the line
require 'rake/testtask'
require 'rake/rdoctask'
below "require 'rake'" in Rakefile and it worked.

Friday, February 25, 2011

Getting error uninitialized constant ENV_PATH (NameError) on running rails console

My Rails version is - 3.0.3
Ruby version - ruby 1.8.7 (2010-08-16 patchlevel 302) [i386-mingw32]
Gem version - 1.3.7
Operating system - Windows 7

When I run command "rails console" it gives following error:

C:\projects\working project\demo>rails console
C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.0.beta3/lib/rails/commands.rb:33: uninitialized constant ENV_PATH (NameError)
        from script/rails:6:in `require'
        from script/rails:6

Note -"Rails console" is not working only for this project and only on Windows 7
("Rails console" for the above project demo is working fine on linux but not working on Windows 7.
"Rails console" on another application on Rails 3 is working fine on Windows 7)

Can anybody guide me how to solve this error?

Thursday, February 24, 2011

"rake rails:upgrade:routes" create "postget" undefined method while upgrading from Rails 2.3.5 to 3.0.3

I am using "https://github.com/rails/rails_upgrade" plugin to analyze the files, actions, code that needs to be modified because of Rails upgradation.

I have routes as such:
map.resources :users, :collection=>{:billable=>[:post,:get]}
When ran the command "rake rails:upgrade:routes", it creates the new routes as :
resources :users do
  collection do
    postget :billable 
  end
end
Here, postget is undefined method.
To fix this, change the code as:
resources :users do
  collection do
    get :billable
    post :billable
  end
end

Alternate way to install rails_upgrade plugin

Alternate way to install rails_upgrade plugin when getting error "ruby: No such file or directory -- script/plugin (LoadError)" while installing the plugin:
While running the command   
ruby script/plugin install git://github.com/rails/rails_upgrade.git
getting error 
ruby: No such file or directory -- script/plugin (LoadError).
Alternate way to install the plugin is to visit the site 
"https://github.com/rails/rails_upgrade"

and download the plugin.Extract the zip folder, you will get a folder named "rails-rails_upgrade-56b0337" inside folder "rails-rails_upgrade-56b0337".

Rename the inside folder from "rails-rails_upgrade-56b0337" to "rails_upgrade", copy and place the folder inside "vendor\plugins" folder. 

So Simple and your plugin is ready to be used.....

Wednesday, February 23, 2011

Analyzing the upgradation of Rails application from 2.3.5 to 3.0.3

I have an existing Rails application created on Rails version 2.3.5. I now want to upgrade it to Rails version 3.0.3. But first I want to analyze what are the points I need to consider, which are the areas that need more focus.
I found an official plugin for Rails upgrade named: rails_upgrade
To install the plugin run the command:
ruby script/plugin install git://github.com/rails/rails_upgrade.git
The plugin adds the following tasks:
  • rake rails:upgrade:check - Check your app for required upgrades
  • rake rails:upgrade:backup - Backup your likely modified files that might be overwritten by the generator
  • rake rails:upgrade:routes  - Generate a new route file
  • rake rails:upgrade:gems - Generate a Gemfile from your config.gem directives
  • rake rails:upgrade:configuration - Generate code for a new config/application.rb from your environment.rb
You can also refer to link- "http://omgbloglol.com/post/353978923/the-path-to-rails-3-approaching-the-upgrade" which has documented the upgrade in detailed way.

Getting error: undefined method `to_key' for #

Rails 3 and Authlogic together giving error :
undefined method `to_key' for #<UserSession: no credentials provided>
To fix the error, change the code in user_session.rb as given below:

class UserSession < Authlogic::Session::Base

 def to_key
   new_record? ? nil : [ self.send(self.class.primary_key) ]
  end
end

And it will work fine.
For details, you can refer to http://techoctave.com/c7/posts/37-authlogic-and-rails-3-0-solution

CodeConf 2011 :GitHub conference on April 9th & 10th in San Francisco


CodeConf2011 is about improving the software ecosystem through best practices. GitHub is putting on a conference on April 9th & 10th in San Francisco. You can register at $499, only 300 spots are available. List of speakers are yet not finalized, but you can have latest up-to-date info at http://twitter.com/codeconf. For further details, visit the site - http://codeconf.com/

Wednesday, February 16, 2011

Date Time Formats in Ruby on Rails

Different types of Date Time Format with their meaning:

  %a - The abbreviated weekday name (``Sun'') 
  %b - The abbreviated month name (``Jan'')
  %c - The preferred local date and time representation
  %d - Day of the month (01..31)
  %e - Day of the month without a leading zero (1..31)
  %j - Day of the year (001..366)
  %m - Month of the year (01..12)
  %p - Meridian indicator (``AM''  or  ``PM'')
  %w - Day of the week (Sunday is 0, 0..6)
  %x - Preferred representation for the date alone, no time
  %y - Year without a century (00..99)

  %A - The  full  weekday  name (``Sunday'')
  %B - The  full  month  name (``January'')
  %H - Hour of the day, 24-hour clock (00..23)
  %I - Hour of the day, 12-hour clock (01..12) 
  %M - Minute of the hour (00..59)
  %S - Second of the minute (00..60)
  %U - Week  number  of the current year,
  starting with the first Sunday as the first day of the first week (00..53)

  %W - Week  number  of the current year,
  starting with the first Monday as the first day of the first week (00..53)
 
  %X - Preferred representation for the time alone, no date
  %Y - Year with century
  %Z - Time zone name
  %% - Literal ``%'' character

Examples to use them:

   t = Time.now                       #=> "Wed Feb 16 17:04:52 +0530 2011"
   t.strftime("%d-%m-%Y")                         #=> "16-02-2011"
   t.strftime("at %I:%M%p")                        #=> "at 05:07PM"
   t.strftime("%m/%d/%Y %H:%M:%S")     #=> "02/16/2011 17:08:40"

Tuesday, February 15, 2011

Registration Open for RailsConf May 16 - 19, 2011 in Baltimore

After a huge success of RailsConf 2010 held in Baltimore, once again RailsConf is ready to bang this year. RailsConf 2011 will be held in Baltimore Convention Center between May 16-19. If you are really excited about it and want to be a part of conference, then you have to be submit proposals by 17 Feb. Ruby Central and O'Reilly Media are seeking leaders for RailsConf sessions and tutorials.

You can refer to site http://en.oreilly.com/rails2011 for further details.

Thursday, February 10, 2011

named_scope DEPRECATION WARNING on Rails 3.0.3

While using named_scope in Rails 3.0.3
class Project < ActiveRecord::Base
  named_scope :completed,:conditions=>"finish = true" 
end
you will get DEPRECATION WARNING.
DEPRECATION WARNING: Base.named_scope has been deprecated, please use Base.scope instead. (called from C:/projects/myapp/app/models/project.rb:2)
 To fix this use scope instead of named_scope. Your new code will be:

class Project < ActiveRecord::Base
  scope :completed,:conditions=>"finish = true" 
end

To pass current_user object to named_scope in model

named_scope is a way to search records from table repeatedly. Its written in model and one of the easiest way to fetch records.
For example to fetch records from projects table with status finish true, we can proceed as follows:
class Project < ActiveRecord::Base
  named_scope :completed,:conditions=>"finish = true" 
end
And to use it we can call model name dot named_scope method :
Project.completed
Its very simple, but to pass current_user object to this named_scope is a bit tricky. Suppose we have to find all the projects with status finish, of the current user then it can be done by taking user as an object in it passing user.id as displayed in the below code:

class Project < ActiveRecord::Base 
  named_scope :completed,lambda { |user| where("finish=? and user_id = ?", true, user.id)} 
end

And to use we have to pass the current_user object as:
Project.completed(current_user)
Simple, isn't it !

Bundle install on linux giving ERROR: Failed to build gem native extension. (Gem::Installer::ExtensionBuildError)

Ruby version - 1.8.7, Rails version - 3.0.3
While running command bundle install on linux, getting following errors:

Installing bcrypt-ruby (2.1.2) with native extensions /usr/local/lib/site_ruby/1.8/rubygems/installer.rb:483:in `build_extensions': ERROR: Failed to build gem native extension. (Gem::Installer::ExtensionBuildError)

/usr/bin/ruby1.8 extconf.rb
extconf.rb:13:in `require': no such file to load -- mkmf (LoadError)
from extconf.rb:13


Gem files will remain installed in /usr/lib/ruby/gems/1.8/gems/bcrypt-ruby-2.1.2 for inspection.
Results logged to /usr/lib/ruby/gems/1.8/gems/bcrypt-ruby-2.1.2/ext/mri/gem_make.out
from /usr/local/lib/site_ruby/1.8/rubygems/installer.rb:446:in `each'
from /usr/local/lib/site_ruby/1.8/rubygems/installer.rb:446:in `build_extensions'
from /usr/local/lib/site_ruby/1.8/rubygems/installer.rb:198:in `install'
from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.7/lib/bundler/source.rb:95:in `install'
from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.7/lib/bundler/installer.rb:55:in `run'
from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.7/lib/bundler/spec_set.rb:12:in `each'
from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.7/lib/bundler/spec_set.rb:12:in `each'
from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.7/lib/bundler/installer.rb:44:in `run'
from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.7/lib/bundler/installer.rb:8:in `install'
from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.7/lib/bundler/cli.rb:225:in `install'
from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.7/lib/bundler/vendor/thor/task.rb:22:in `send'
from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.7/lib/bundler/vendor/thor/task.rb:22:in `run'
from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.7/lib/bundler/vendor/thor/invocation.rb:118:in `invoke_task'
from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.7/lib/bundler/vendor/thor.rb:246:in `dispatch'
from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.7/lib/bundler/vendor/thor/base.rb:389:in `start'
from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.7/bin/bundle:13
from /usr/bin/bundle:19:in `load'
from /usr/bin/bundle:19


Any suggestion on this?