Saturday, December 24, 2011

What's new in Edge Rails: EXPLAIN

What's new in Edge Rails: EXPLAIN:
There are some new features related to EXPLAIN in the forthcoming Ruby on Rails 3.2 we'd like
to share:
  • Running EXPLAIN manually
  • Automatic EXPLAIN for slow queries
  • Silencing automatic EXPLAIN
As of this writing they are available for the adapters sqlite3, mysql2, and postgresql.

 

Running EXPLAIN Manually

You can now run EXPLAIN on the SQL generated by a relation this way:
User.where(:id => 1).joins(:posts).explain
  
The result of that method call is a string that carefully imitates the output of database shells.

Please note that explain runs the query or queries and asks the database for their respective query plan afterwards. This is because due to eager loading a relation may trigger several queries to fetch the records and their associations, and in such cases some queries need the result of the previous ones.

If the relation triggers several queries the method still returns a single string with all the query plans. This is an output meant for human consumption so we preferred to present everything as a string in a format which is familiar right away rather than a structure.

Automatic EXPLAIN For Slow Queries

Rails 3.2 has the ability to help in detecting slow queries.
New applications get
config.active_record.auto_explain_threshold_in_seconds = 0.5
in config/environments/development.rb. Active Record monitors queries and if they take more than that threshold their query plan will be logged using warn.

That works for anything running find_by_sql (which is almost everything, since most of Active Record ends up calling that method). In the particular case of relations, the threshold is compared against the total time needed to fetch the records, not against the time taken by each individual query. Because conceptually we are concerned with the cost of the call
User.where(:id => 1).joins(:posts).explain
rather than the cost of the different queries that call may trigger due to the implementation.
By default the threshold is nil in the test and production environments, which means the feature is disabled.

The value of that parameter is nil also if the threshold is not set, so existing applications will need to add it by hand if they migrate to 3.2 to be able to enable automatic EXPLAIN.

Silencing Automatic EXPLAIN

With automatic EXPLAIN enabled, it could still be the case that some queries are just slow and you know they have to be. For example, a heavyweight report in the backoffice.
The macro silence_auto_explain allows you to avoid having EXPLAIN run repeatedly in those areas of code:

ActiveRecord::Base.silence_auto_explain do
 # no automatic EXPLAIN here
end

Interpreting Query Plans

The interpretation of the query plans is another topic, these are some pointers:
Happy debugging!

Tuesday, December 13, 2011

Installing ZeroMQ for Rubyists on Ubuntu

To install zmq gem in Ruby on Rails on Ubuntu, execute the command:

sudo apt-get install zeromq-bin libzmq-dbg libzmq-dev libzmq0

then

sudo gem install zmq

Saturday, December 3, 2011

Getting error "Illegal octal digit"

While trying to execute below case method to display month, getting 'Illegal octal digit' for August and September month only:

def self.convert_month(month)
    case month
      when "Jan" :         01
      when "Feb" :        02
      when "Mar" :        03
      when "Apr" :        04
      when "May" :       05
      when "Jun" :         06
      when "Jul" :          07
      when "Aug" :       08
      when "Sep" :        09
      when "Oct" :        10
      when "Nov" :       11
      when "Dec" :        
  end

Solution

Ruby is interpreting numbers that have a leading 0 as being in octal (base 8). Thus the digits 8 and 9 are not valid. So,  I removed leading zero from Aug and Sep month and changed the method as:

when "Aug" :        8
when "Sep" :         9

Friday, November 11, 2011

Getting error "[BUG] unknown type 0x22 (0xc given)" with ZMQ

Installed ZMQ successfully, but while testing ZMQ commands from rails console got error.


ritesh@ritesh:~/projects/zmqtest$ rails c
Loading development environment (Rails 3.0.0)
ruby-1.9.2-p290 :001 > require "zmq"
 => ["ZMQ"]
ruby-1.9.2-p290 :002 > c = ZMQ::Context.new
 => #<ZMQ::Context:0xbbf18cc>
ruby-1.9.2-p290 :003 > req = c.socket ZMQ::REQ
(irb):3: [BUG] unknown type 0x22 (0xc given)
ruby 1.9.2p290 (2011-07-09 revision 32553) [i686-linux]


-- control frame ----------
c:0029 p:---- s:0105 b:0105 l:000104 d:000104 CFUNC  :socket
c:0028 p:0020 s:0101 b:0101 l:00264c d:000eb8 EVAL   (irb):3
c:0027 p:---- s:0098 b:0098 l:000097 d:000097 FINISH
c:0026 p:---- s:0096 b:0096 l:000095 d:000095 CFUNC  :eval
c:0025 p:0028 s:0089 b:0089 l:000088 d:000088 METHOD /home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/irb/workspace.rb:80
c:0024 p:0033 s:0082 b:0081 l:000080 d:000080 METHOD /home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/irb/context.rb:254
c:0023 p:0031 s:0076 b:0076 l:002684 d:000075 BLOCK  /home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/irb.rb:159
c:0022 p:0042 s:0068 b:0068 l:000067 d:000067 METHOD /home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/irb.rb:273
c:0021 p:0011 s:0063 b:0063 l:002684 d:000062 BLOCK  /home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/irb.rb:156
c:0020 p:0144 s:0059 b:0059 l:000042 d:000058 BLOCK  /home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/irb/ruby-lex.rb:243
c:0019 p:---- s:0056 b:0056 l:000055 d:000055 FINISH
c:0018 p:---- s:0054 b:0054 l:000053 d:000053 CFUNC  :loop
c:0017 p:0009 s:0051 b:0051 l:000042 d:000050 BLOCK  /home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/irb/ruby-lex.rb:229
c:0016 p:---- s:0049 b:0049 l:000048 d:000048 FINISH
c:0015 p:---- s:0047 b:0047 l:000046 d:000046 CFUNC  :catch
c:0014 p:0023 s:0043 b:0043 l:000042 d:000042 METHOD /home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/irb/ruby-lex.rb:228
c:0013 p:0046 s:0040 b:0040 l:002684 d:002684 METHOD /home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/irb.rb:155
c:0012 p:0011 s:0037 b:0037 l:0016cc d:000036 BLOCK  /home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/irb.rb:70
c:0011 p:---- s:0035 b:0035 l:000034 d:000034 FINISH
c:0010 p:---- s:0033 b:0033 l:000032 d:000032 CFUNC  :catch
c:0009 p:0183 s:0029 b:0029 l:0016cc d:0016cc METHOD /home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/irb.rb:69
c:0008 p:0210 s:0024 b:0024 l:000e18 d:000e18 METHOD /home/ritesh/.rvm/gems/ruby-1.9.2-p290@ss2.0/gems/railties-3.0.0/lib/rails/commands/console.rb:44
c:0007 p:0019 s:0020 b:0020 l:000019 d:000019 METHOD /home/ritesh/.rvm/gems/ruby-1.9.2-p290@ss2.0/gems/railties-3.0.0/lib/rails/commands/console.rb:8
c:0006 p:0430 s:0016 b:0016 l:000015 d:000015 TOP    /home/ritesh/.rvm/gems/ruby-1.9.2-p290@ss2.0/gems/railties-3.0.0/lib/rails/commands.rb:23
c:0005 p:---- s:0012 b:0012 l:000011 d:000011 FINISH
c:0004 p:---- s:0010 b:0010 l:000009 d:000009 CFUNC  :require
c:0003 p:0061 s:0006 b:0006 l:000964 d:000434 EVAL   script/rails:6
c:0002 p:---- s:0004 b:0004 l:000003 d:000003 FINISH
c:0001 p:0000 s:0002 b:0002 l:000964 d:000964 TOP  
---------------------------
-- Ruby level backtrace information ----------------------------------------
script/rails:6:in `<main>'
script/rails:6:in `require'
/home/ritesh/.rvm/gems/ruby-1.9.2-p290@ss2.0/gems/railties-3.0.0/lib/rails/commands.rb:23:in `<top (required)>'
/home/ritesh/.rvm/gems/ruby-1.9.2-p290@ss2.0/gems/railties-3.0.0/lib/rails/commands/console.rb:8:in `start'
/home/ritesh/.rvm/gems/ruby-1.9.2-p290@ss2.0/gems/railties-3.0.0/lib/rails/commands/console.rb:44:in `start'
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/irb.rb:69:in `start'
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/irb.rb:69:in `catch'
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/irb.rb:70:in `block in start'
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/irb.rb:155:in `eval_input'
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/irb/ruby-lex.rb:228:in `each_top_level_statement'
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/irb/ruby-lex.rb:228:in `catch'
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in `block in each_top_level_statement'
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in `loop'
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/irb/ruby-lex.rb:243:in `block (2 levels) in each_top_level_statement'
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/irb.rb:156:in `block in eval_input'
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/irb.rb:273:in `signal_status'
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/irb.rb:159:in `block (2 levels) in eval_input'
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/irb/context.rb:254:in `evaluate'
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/irb/workspace.rb:80:in `evaluate'
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/irb/workspace.rb:80:in `eval'
(irb):3:in `irb_binding'
(irb):3:in `socket'


-- C level backtrace information -------------------------------------------
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(rb_vm_bugreport+0x72) [0xb778b702]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x53c57) [0xb7654c57]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(rb_bug+0x3a) [0xb76559ba]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(rb_check_type+0x16e) [0xb765771e]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/i686-linux/zmq.so(+0x1d20) [0xb6ca0d20]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x175b53) [0xb7776b53]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x183da9) [0xb7784da9]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x17ac7b) [0xb777bc7b]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x180048) [0xb7781048]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x180400) [0xb7781400]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(rb_f_eval+0xd9) [0xb7781a19]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x175b78) [0xb7776b78]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x183da9) [0xb7784da9]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x17ac7b) [0xb777bc7b]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x180048) [0xb7781048]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x1849c3) [0xb77859c3]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(rb_rescue2+0x139) [0xb765c319]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x1762a5) [0xb77772a5]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x175b5d) [0xb7776b5d]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x183da9) [0xb7784da9]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x17ac7b) [0xb777bc7b]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x180048) [0xb7781048]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x184a80) [0xb7785a80]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(rb_catch_obj+0xb7) [0xb7778c27]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x177ce6) [0xb7778ce6]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x175b78) [0xb7776b78]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x183da9) [0xb7784da9]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x17ac7b) [0xb777bc7b]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x180048) [0xb7781048]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x184a80) [0xb7785a80]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(rb_catch_obj+0xb7) [0xb7778c27]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x177ce6) [0xb7778ce6]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x175b78) [0xb7776b78]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x183da9) [0xb7784da9]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x17ac7b) [0xb777bc7b]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x180048) [0xb7781048]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(rb_iseq_eval+0x153) [0xb77860b3]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x5d5bf) [0xb765e5bf]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(rb_require_safe+0x692) [0xb765fb42]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(rb_f_require+0x26) [0xb765fe36]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x175b53) [0xb7776b53]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x183da9) [0xb7784da9]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x17ac7b) [0xb777bc7b]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x180048) [0xb7781048]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(rb_iseq_eval_main+0xab) [0xb778619b]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x59f0a) [0xb765af0a]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(ruby_exec_node+0x25) [0xb765be05]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(ruby_run_node+0x35) [0xb765de55]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/bin/ruby(main+0x68) [0x8048748]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xe7) [0xb7427e37]
/home/ritesh/.rvm/rubies/ruby-1.9.2-p290/bin/ruby() [0x8048641]


[NOTE]
You may have encountered a bug in the Ruby interpreter or extension libraries.
Bug reports are welcome.
For details: http://www.ruby-lang.org/bugreport.html


Aborted

Observation
When I put the same commands I was executing on console in a .rb file and executed, it successfully executed it, so what I found is it cannot be executed line by line from console, requires a complete ruby script file.

Tuesday, September 6, 2011

Getting error "ArgumentError: wrong number of arguments (3 for 2)" for mysql2_adapter

In one of my application I am using Ruby 1.9.2, mysql2 gem, Rails 3.0.6.

While upgrading to Rails 3.1, getting following error throughout the application. When I ran command on console, then also getting same error:

ruby-1.9.2-p290 :003 > c = Cuisine.all
ArgumentError: wrong number of arguments (3 for 2)
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/mysql2-0.2.13/lib/active_record/connection_adapters/mysql2_adapter.rb:585:in `select'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:18:in `select_all'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activerecord-3.1.0/lib/active_record/base.rb:470:in `find_by_sql'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activerecord-3.1.0/lib/active_record/relation.rb:111:in `to_a'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activerecord-3.1.0/lib/active_record/relation/finder_methods.rb:155:in `all'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activerecord-3.1.0/lib/active_record/base.rb:441:in `all'
    from (irb):3
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/railties-3.1.0/lib/rails/commands/console.rb:45:in `start'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/railties-3.1.0/lib/rails/commands/console.rb:8:in `start'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/railties-3.1.0/lib/rails/commands.rb:40:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

Solution to fix the problem:

In Gemfile, replace 'mysql2' line with

       gem 'mysql2', '>= 0.3'

and install mysql2 greater than '0.3' version, in my case it is ' mysql2 (0.3.6) ' and it will work :)


NOTE: as of Rails 0.3.0, and ActiveRecord 3.1 - the ActiveRecord adapter has been pulled out of mysql2 gem and into ActiveRecord itself. If you need to use mysql2 with Rails versions < 3.1 make sure and specify gem "mysql2", "~> 0.2.7" in your Gemfile

And for Rails version 3.1 or above use  gem 'mysql2', '>= 0.3'

Sunday, September 4, 2011

Rails 3.1.0 has been released!

Rails 3.1.0 has been released!:
Hi everybody!
It's been 3 Months since RailsConf, so I think it's time we released Rails 3.1.0. So, here it is! I've released Rails 3.1.0!

CHANGES

For a much more attractive and easy to read list of changes, please check out the awesome Rails 3.1.0 Release Notes on the Rails Guides site. For a less attractive list of changes, please continue to read!

Here are some highlights of the major changes in Rails 3.1.0:

ActionPack


  • ActionPack has been updated to include the new asset pipeline. Please see the rails guides on the asset pipeline.
  • Streaming response support has been added. This feature allows you to stream templates to the user before processing has actually finished. See the Rails Guides, or documentation in ActionController::Metal::Streaming for more information. Middleware have been refactored to support this feature.
  • RJS has been extracted to a gem.

ActiveModel

  • attr_accessible and friends now accepts :as as option to specify a role
  • Added ActiveModel::SecurePassword to encapsulate dead-simple password usage with BCrypt encryption and salting.

ActiveRecord

  • Prepared statement caches have been integrated. ActiveRecord::Base#create and simple finders will use a prepared statement and cache for more performant inserts and selects.
  • Associations have been refactored for greater simplicity and maintainability.
  • default_scope can take any object that responds to call.
  • PostgreSQL adapter only supports PostgreSQL version 8.2 and higher.
  • Migrations use instance methods rather than class methods. Rather than defining a self.up method, you should define an instance method up.
  • Migrations are reversible. When a new migration is generated, the migration will contain one method called change. Database changes made in this method will automatically know how to reverse themselves. For more information, see the documentation for ActiveRecord::Migration and ActiveRecord::Migration::CommandRecorder.
  • When a model is generated, add_index is added by default for belongs_to or references columns.

ActiveResource

  • The default format has been changed to JSON for all requests. If you want to continue to use XML you will need to set self.format = :xml in the class.

ActiveSupport

  • ActiveSupport::BufferedLogger set log encoding to BINARY, but still use text mode to output portable newlines.
  • Add Object#in? to test if an object is included in another object.
  • ActiveSupport::Dependencies::ClassCache class has been introduced for holding references to reloadable classes.
  • Added weeks_ago and prev_week to Date/DateTime/Time.
  • JSON decoding now uses the multi_json gem which also vendors a json engine called OkJson. The yaml backend has been removed in favor of OkJson as a default engine for 1.8.x, while the built in 1.9.x json implementation will be used by default.

Railties

  • The default database schema file is written as UTF-8.
  • Rack::Sendfile middleware is used only if x_sendfile_header is present.
  • Add alias r for rails runner.
  • jQuery is the new default JavaScript library.
  • Added config.force_ssl configuration which loads Rack::SSL middleware and force all requests to be under HTTPS protocol

For more info

For a more detailed list of changes, please see each of the CHANGELOG files checked in to the Rails repository on github.
For an even more detailed list of changes, please see the commit list between Rails 3.0.10 and 3.1.0.

The End

I am personally very proud of this release. I want to say thank you to the people testing our release candidates, the people submitting patches and sending in bug reports. I think that Rails 3.1.0 is the best release of Rails to date, and we could not have done it without you.
Please continue to create amazing things with this framework!

SHA-1

  • b68f74ced662145a4139409edf3c51db1159ead8 actionmailer-3.1.0.gem
  • 136474f270677ae75ad0f9599d26e89cf1d4bc7b actionpack-3.1.0.gem
  • e6b68453c08bb0da52ed1d422ba2f87a5e3aa794 activemodel-3.1.0.gem
  • dfbae15c0d395304812c22fbf18aa9daadbe20b4 activerecord-3.1.0.gem
  • 3f1f547e500d1ffc1f7c3ee4ab9eb1526157a870 activeresource-3.1.0.gem
  • f21627c2f429abfa8685d2147fadab6704c13869 activesupport-3.1.0.gem
  • 21c6592189fb358a066846754a8f7ce7a238fca6 rails-3.1.0.gem
  • 79cfa1eca232de9a45453829287e4438089b7955 railties-3.1.0.gem
<3 <3 <3

'Invalid date' error with Ruby 1.9.2

In one of my Rails 3.0 application, when migrated to Ruby 1.9.2, I am getting "invalid date" error sometimes.
Here is the detail error report:

invalid date
/Users/ror/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/date.rb:809:in `civil'
/Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activesupport-3.0.5/lib/active_support/core_ext/string/conversions.rb:44:in `to_date'
/Users/ror/projects/restaurant/lib/tasks/import_csv.rake:529:in `block (4 levels) in <top (required)>'
/Users/ror/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/csv.rb:1768:in `each'
/Users/ror/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/csv.rb:1202:in `block in foreach'
/Users/ror/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/csv.rb:1340:in `open'
/Users/ror/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/csv.rb:1201:in `foreach'
/Users/ror/projects/restaurant/lib/tasks/import_csv.rake:513:in `block (3 levels) in <top (required)>'
/Users/ror/projects/restaurant/lib/tasks/import_csv.rake:504:in `each'
/Users/ror/projects/restaurant/lib/tasks/import_csv.rake:504:in `block (2 levels) in <top (required)>'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/task.rb:205:in `call'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/task.rb:205:in `block in execute'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/task.rb:200:in `each'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/task.rb:200:in `execute'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/Users/ror/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/task.rb:144:in `invoke'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/application.rb:112:in `invoke_task'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/application.rb:90:in `block (2 levels) in top_level'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/application.rb:90:in `each'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/application.rb:90:in `block in top_level'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/application.rb:129:in `standard_exception_handling'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/application.rb:84:in `top_level'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/application.rb:62:in `block in run'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/application.rb:129:in `standard_exception_handling'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/application.rb:59:in `run'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/bin/rake:32:in `<top (required)>'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/bin/rake:19:in `load'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/bin/rake:19:in `<main>'
Tasks: TOP => import:csv

Some Observation
ruby-1.9.2-p290 :030 > a = '18/06/2011'
 => "18/06/2011"
ruby-1.9.2-p290 :031 > Date.parse(a)
 => Sat, 18 Jun 2011
ruby-1.9.2-p290 :032 > a = '06/18/2011'
 => "06/18/2011"
ruby-1.9.2-p290 :033 > Date.parse(a)
ArgumentError: invalid date
    from /Users/ror/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/date.rb:1022:in `new_by_frags'
    from /Users/ror/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/date.rb:1066:in `parse'
    from (irb):33
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/railties-3.0.5/lib/rails/commands/console.rb:44:in `start'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/railties-3.0.5/lib/rails/commands/console.rb:8:in `start'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/railties-3.0.5/lib/rails/commands.rb:23:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

Conclusion
Ruby 1.9.2 accepts date format "dd/mm/yyyy" not "mm/dd/yyyy"

Getting error 'Could not update iceauthority file' in ubuntu

After logging in to an Ubuntu an ICEauthority error appeared. 

Solution
Press Ctrl-Alt-F1 to get a console. There, I hit Enter to get a login prompt. I logged in with my username and password, and this gave me a regular command prompt. After trying several failed commands, I found the following command on Ubuntuforums.org that helped me to login successfully.

Open a terminal and enter the following command:
sudo chown -R user:user /home/user/.*

Replace user with your username. For example, My username is 'ritesh' so I typed command:
 

sudo chown -R ritesh:ritesh /home/ritesh/.*
After issuing the command, log out and log back in and Ubuntu will be back to normal GUI screen. This error came because of sftp. I installed sftp and assigned my username to use sftp that caused this error to occur. 


Related Links:
- http://ubuntuforums.org/showthread.php?t=1081730&highlight=ICEAuthority&page=2

Wednesday, August 24, 2011

Getting error "couldn't load file "/opt/local/share/macports/Tcl/macports1.0/MacPorts.dylib" while installing MacPort

Installed MacPorts from the DMG package at http://www.macports.org/install.php
After installing it, when tried to check the port version or install ImageMagick displaying error:

:restaurant ror$ sudo port install ImageMagick
couldn't load file "/opt/local/share/macports/Tcl/macports1.0/MacPorts.dylib": dlopen(/opt/local/share/macports/Tcl/macports1.0/MacPorts.dylib, 10): no suitable image found.  Did find:
    /opt/local/share/macports/Tcl/macports1.0/MacPorts.dylib: unknown required load command 0x80000022
    while executing
"load /opt/local/share/macports/Tcl/macports1.0/MacPorts.dylib"
    ("package ifneeded" script)
    invoked from within
"package require macports"
    (file "/opt/local/bin/port" line 38)


Solution to fix this problem:
Installed it from source

tar xjvf MacPorts-2.0.1.tar.bz2
cd MacPorts-2.0.1
./configure && make && sudo make install
sudo port -v selfupdate

Now you can check port version by command:
              port version
which will give you
             Version: 2.0.1

Installing ImageMagick on Mac

Installing ImageMagick on Mac is done very easily using MacPorts.
First Install MacPorts from source given at ' http://www.macports.org/install.php'
And then install ImageMagick

To install MacPort:
1. Download source from  ' http://www.macports.org/install.php'
2. tar xjvf MacPorts-2.0.1.tar.bz2
3. cd MacPorts-2.0.1
4  ./configure && make && sudo make install
5. sudo port -v selfupdate

You can check the port version by command "port verison"

To install ImageMagick
After successfully installing port, run the command:
-   sudo port install ImageMagick
to install ImageMagick

Tuesday, August 23, 2011

Getting error "`require': no such file to load -- pony (LoadError)" in SMSFu with Rails 3.0.5

I am integrating SMS system with Rails 3.0.5. As we wanted a free solution for sending SMS, we went through SMSFu. But when we tried to run the application after installing 'sms_fu', faced issues which was patched and now able to run the Rails 3.0.5 application with SMSFu. Here are the details:

Installed Plugin:
git clone git://github.com/brendanlim/sms-fu.git vendor/plugins/sms_fu  
And tried to start the server, got following error:

 -:restaurant ror$ rails s
=> Booting WEBrick
=> Rails 3.0.5 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:239:in `require': no such file to load -- pony (LoadError)
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:239:in `block in require'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:225:in `block in load_dependency'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:596:in `new_constants_in'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:225:in `load_dependency'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:239:in `require'
    from /Users/ror/projects/restaurant/vendor/plugins/sms_fu/lib/sms_fu.rb:2:in `<top (required)>'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:239:in `require'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:239:in `block in require'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:225:in `block in load_dependency'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:596:in `new_constants_in'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:225:in `load_dependency'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:239:in `require'
    from /Users/ror/projects/restaurant/vendor/plugins/sms_fu/rails/init.rb:1:in `block in <class:Plugin>'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/railties-3.0.5/lib/rails/plugin.rb:81:in `eval'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/railties-3.0.5/lib/rails/plugin.rb:81:in `block in <class:Plugin>'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/railties-3.0.5/lib/rails/initializable.rb:25:in `instance_exec'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/railties-3.0.5/lib/rails/initializable.rb:25:in `run'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/railties-3.0.5/lib/rails/initializable.rb:50:in `block in run_initializers'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/railties-3.0.5/lib/rails/initializable.rb:49:in `each'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/railties-3.0.5/lib/rails/initializable.rb:49:in `run_initializers'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/railties-3.0.5/lib/rails/application.rb:134:in `initialize!'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/railties-3.0.5/lib/rails/application.rb:77:in `method_missing'
    from /Users/ror/projects/restaurant/config/environment.rb:6:in `<top (required)>'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:239:in `require'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:239:in `block in require'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:225:in `block in load_dependency'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:596:in `new_constants_in'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:225:in `load_dependency'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:239:in `require'
    from /Users/ror/projects/restaurant/config.ru:3:in `block in <main>'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/rack-1.2.1/lib/rack/builder.rb:46:in `instance_eval'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/rack-1.2.1/lib/rack/builder.rb:46:in `initialize'
    from /Users/ror/projects/restaurant/config.ru:1:in `new'
    from /Users/ror/projects/restaurant/config.ru:1:in `<main>'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/rack-1.2.1/lib/rack/builder.rb:35:in `eval'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/rack-1.2.1/lib/rack/builder.rb:35:in `parse_file'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/rack-1.2.1/lib/rack/server.rb:162:in `app'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/rack-1.2.1/lib/rack/server.rb:248:in `wrapped_app'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/rack-1.2.1/lib/rack/server.rb:213:in `start'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/railties-3.0.5/lib/rails/commands/server.rb:65:in `start'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/railties-3.0.5/lib/rails/commands.rb:30:in `block in <top (required)>'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/railties-3.0.5/lib/rails/commands.rb:27:in `tap'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/railties-3.0.5/lib/rails/commands.rb:27:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

Then I tried to run the application with 'sms_fu' gem, in Gemfile added:
gem 'sms_fu'

Executed Bundle install. It installed sms_fu version '1.1.2' successfully, when starting the application gives following errors:

:restaurant ror$ rails s
/Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:239:in `require': no such file to load -- pony (LoadError)
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:239:in `block in require'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:225:in `block in load_dependency'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:596:in `new_constants_in'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:225:in `load_dependency'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:239:in `require'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/sms_fu-1.1.2/lib/sms_fu.rb:2:in `<top (required)>'
    from /Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.15/lib/bundler/runtime.rb:68:in `require'
    from /Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.15/lib/bundler/runtime.rb:68:in `block (2 levels) in require'
    from /Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.15/lib/bundler/runtime.rb:66:in `each'
    from /Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.15/lib/bundler/runtime.rb:66:in `block in require'
    from /Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.15/lib/bundler/runtime.rb:55:in `each'
    from /Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.15/lib/bundler/runtime.rb:55:in `require'
    from /Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.15/lib/bundler.rb:120:in `require'
    from /Users/ror/projects/restaurant/config/application.rb:7:in `<top (required)>'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/railties-3.0.5/lib/rails/commands.rb:28:in `require'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/railties-3.0.5/lib/rails/commands.rb:28:in `block in <top (required)>'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/railties-3.0.5/lib/rails/commands.rb:27:in `tap'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/railties-3.0.5/lib/rails/commands.rb:27:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'



 Solution or you can say workaround to handle this problem:

I commented "require 'pony'" line2 in "vendor/plugins/sms_fu/lib/sms_fu.rb" and then able to run the application.

Friday, July 29, 2011

Difference between Application server and Web Server

apache, nginx, IIS are web servers
mongrel, webrick, phusion passenger are app servers

App server is something which works with particular programming language and parses and executes the code
since mongrel and webrick can only work with rails, so they are app servers

Web servers are servers which can take the request from the browser.
Web servers normally works on port 80 though we can change the port in configuration
since mongrel and webrick can take that request directly, so they can be thought of as web servers but web servers do have a lot of other functionality like request pipeline, load balancing etc.
App servers lack these functionalities.

About Mongrel server:
mongrel work as web as well as app server if you are talking about dev environment
but in production, mongrel alone can not work it will be too slow
so we need a web server in front of mongrel

Thanks to Mr. Sumit  Garg & Mr. Sidhant for clarifying these points in details.
I would love to have others view also on this.

Difference between Validations, Callbacks and Observers

Validations allow you to ensure that only valid data is stored in your database.
Example: validates_presence_of :user_name, :password
                 validates_numericality_of :value

We can write custom validation also as

def validate
  errors.add(:price, “should be a positive value”) if price.nil?|| price < 0.01
end

Callbacks and observers allow you to trigger logic before or after an alteration of an object’s state.

Callbacks are methods that get called at certain moments of an object’s life cycle. With callbacks it’s possible to write code that will run whenever an Active Record object is created, saved, updated, deleted, validated, or loaded from the database.

Callbacks are hooks into the life cycle of an Active Record object that allow you to trigger logic before or after an alteration of the object state. This can be used to make sure that associated and dependent objects are deleted when destroy is called (by overwriting before_destroy) or to massage attributes before they’re validated (by overwriting before_validation)

Observers are similar to callbacks, but with important differences. Whereas callbacks can pollute a model with code that isn’t directly related to its purpose, observers allow you to add the same functionality outside of a model. For example, it could be argued that a User model should not include code to send registration confirmation emails. Whenever you use callbacks with code that isn’t directly related to your model, you may want to consider creating an observer instead.

Getting error "undefined method `debug_rjs=' for ActionView::Base:Class (NoMethodError)"

Successfully installed RVM, Ruby 1.9.2 with RVM, Rails 3.2.0.beta
While upgrading to Rails 3.2.0.beta on Mac, getting following error:

/Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/thinking-sphinx-ea7673a0188f/lib/thinking_sphinx/search.rb:623: warning: encoding option is ignored - u
=> Booting WEBrick
=> Rails 3.2.0.beta application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/actionpack/lib/action_view/railtie.rb:34:in `block (3 levels) in <class:Railtie>': undefined method `debug_rjs=' for ActionView::Base:Class (NoMethodError)
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/actionpack/lib/action_view/railtie.rb:33:in `each'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/actionpack/lib/action_view/railtie.rb:33:in `block (2 levels) in <class:Railtie>'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/activesupport/lib/active_support/lazy_load_hooks.rb:36:in `instance_eval'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/activesupport/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/activesupport/lib/active_support/lazy_load_hooks.rb:26:in `on_load'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/actionpack/lib/action_view/railtie.rb:32:in `block in <class:Railtie>'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/railties/lib/rails/initializable.rb:25:in `instance_exec'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/railties/lib/rails/initializable.rb:25:in `run'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/railties/lib/rails/initializable.rb:50:in `block in run_initializers'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/railties/lib/rails/initializable.rb:49:in `each'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/railties/lib/rails/initializable.rb:49:in `run_initializers'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/railties/lib/rails/application.rb:92:in `initialize!'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/railties/lib/rails/railtie/configurable.rb:30:in `method_missing'
    from /Users/ror/projects/restaurant/config/environment.rb:6:in `<top (required)>'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/activesupport/lib/active_support/dependencies.rb:236:in `require'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/activesupport/lib/active_support/dependencies.rb:236:in `block in require'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/activesupport/lib/active_support/dependencies.rb:222:in `block in load_dependency'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/activesupport/lib/active_support/dependencies.rb:616:in `new_constants_in'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/activesupport/lib/active_support/dependencies.rb:222:in `load_dependency'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/activesupport/lib/active_support/dependencies.rb:236:in `require'
    from /Users/ror/projects/restaurant/config.ru:4:in `block in <main>'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/rack-1.3.2/lib/rack/builder.rb:51:in `instance_eval'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/rack-1.3.2/lib/rack/builder.rb:51:in `initialize'
    from /Users/ror/projects/restaurant/config.ru:1:in `new'
    from /Users/ror/projects/restaurant/config.ru:1:in `<main>'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/rack-1.3.2/lib/rack/builder.rb:40:in `eval'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/rack-1.3.2/lib/rack/builder.rb:40:in `parse_file'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/rack-1.3.2/lib/rack/server.rb:200:in `app'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/railties/lib/rails/commands/server.rb:46:in `app'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/rack-1.3.2/lib/rack/server.rb:301:in `wrapped_app'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/rack-1.3.2/lib/rack/server.rb:252:in `start'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/railties/lib/rails/commands/server.rb:70:in `start'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/railties/lib/rails/commands.rb:54:in `block in <top (required)>'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/railties/lib/rails/commands.rb:49:in `tap'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/railties/lib/rails/commands.rb:49:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

Solution to fix the problem:
Comment the line in config/environments/development.rb  (if working on development)
# config.action_view.debug_rjs             = true

Getting error "Library not loaded: libmysqlclient.18.dylib (LoadError)" on Mac

On Mac, successfully installed RVM, ruby 1.9.2, mysql2 gem, Rails 3.1.0.rc5.
On starting rails server or rails console, getting following errors:

:restaurant ror$ rails c
/Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/activesupport/lib/active_support/dependencies.rb:236:in `require': dlopen(/Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/mysql2-0.3.6/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.18.dylib (LoadError)
  Referenced from: /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/mysql2-0.3.6/lib/mysql2/mysql2.bundle
  Reason: image not found - /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/mysql2-0.3.6/lib/mysql2/mysql2.bundle
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/activesupport/lib/active_support/dependencies.rb:236:in `block in require'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/activesupport/lib/active_support/dependencies.rb:222:in `block in load_dependency'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/activesupport/lib/active_support/dependencies.rb:616:in `new_constants_in'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/activesupport/lib/active_support/dependencies.rb:222:in `load_dependency'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/activesupport/lib/active_support/dependencies.rb:236:in `require'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/mysql2-0.3.6/lib/mysql2.rb:9:in `<top (required)>'
    from /Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.15/lib/bundler/runtime.rb:68:in `require'
    from /Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.15/lib/bundler/runtime.rb:68:in `block (2 levels) in require'
    from /Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.15/lib/bundler/runtime.rb:66:in `each'
    from /Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.15/lib/bundler/runtime.rb:66:in `block in require'
    from /Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.15/lib/bundler/runtime.rb:55:in `each'
    from /Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.15/lib/bundler/runtime.rb:55:in `require'
    from /Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.15/lib/bundler.rb:120:in `require'
    from /Users/ror/projects/restaurant/config/application.rb:7:in `<top (required)>'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/railties/lib/rails/commands.rb:38:in `require'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/railties/lib/rails/commands.rb:38:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

  Solution to fix the problem:
Type following command and then run Rails server or console:

-:restaurant ror$ export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH

Another way is to create a soft link by command -
sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

Getting error "`method_missing': undefined method `named_scope' for # (NoMethodError)"

In my application, I am using Clicksteam plugin. While upgrading to Rails 3.1.0.rc5, getting following error:

 /Users/ror/projects/restaurant/highline/ruby/1.8/gems/activerecord-3.1.0.rc5/lib/active_record/base.rb:1095:in `method_missing': undefined method `named_scope' for #<Class:0x3668480> (NoMethodError)
    from /Users/ror/projects/restaurant/vendor/plugins/clickstream/lib/app/models/stream.rb:8
    from /Users/ror/projects/restaurant/highline/ruby/1.8/gems/activesupport-3.1.0.rc5/lib/active_support/dependencies.rb:237:in `require'
    from /Users/ror/projects/restaurant/highline/ruby/1.8/gems/activesupport-3.1.0.rc5/lib/active_support/dependencies.rb:237:in `require'
    from /Users/ror/projects/restaurant/highline/ruby/1.8/gems/activesupport-3.1.0.rc5/lib/active_support/dependencies.rb:223:in `load_dependency'
    from /Users/ror/projects/restaurant/highline/ruby/1.8/gems/activesupport-3.1.0.rc5/lib/active_support/dependencies.rb:639:in `new_constants_in'
    from /Users/ror/projects/restaurant/highline/ruby/1.8/gems/activesupport-3.1.0.rc5/lib/active_support/dependencies.rb:223:in `load_dependency'
    from /Users/ror/projects/restaurant/highline/ruby/1.8/gems/activesupport-3.1.0.rc5/lib/active_support/dependencies.rb:237:in `require'
    from /Users/ror/projects/restaurant/vendor/plugins/clickstream/lib/clickstream.rb:3
    from /Users/ror/projects/restaurant/highline/ruby/1.8/gems/activesupport-3.1.0.rc5/lib/active_support/dependencies.rb:237:in `require'
    from /Users/ror/projects/restaurant/highline/ruby/1.8/gems/activesupport-3.1.0.rc5/lib/active_support/dependencies.rb:237:in `require'
    from /Users/ror/projects/restaurant/highline/ruby/1.8/gems/activesupport-3.1.0.rc5/lib/active_support/dependencies.rb:223:in `load_dependency'
    from /Users/ror/projects/restaurant/highline/ruby/1.8/gems/activesupport-3.1.0.rc5/lib/active_support/dependencies.rb:639:in `new_constants_in'
    from /Users/ror/projects/restaurant/highline/ruby/1.8/gems/activesupport-3.1.0.rc5/lib/active_support/dependencies.rb:223:in `load_dependency'
    from /Users/ror/projects/restaurant/highline/ruby/1.8/gems/activesupport-3.1.0.rc5/lib/active_support/dependencies.rb:237:in `require'
    from /Users/ror/projects/restaurant/vendor/plugins/clickstream/rails/init.rb:1
    from /Users/ror/projects/restaurant/highline/ruby/1.8/gems/activesupport-3.1.0.rc5/lib/active_support/dependencies.rb:237:in `require'
    from /Users/ror/projects/restaurant/highline/ruby/1.8/gems/activesupport-3.1.0.rc5/lib/active_support/dependencies.rb:237:in `require'
    from /Users/ror/projects/restaurant/highline/ruby/1.8/gems/activesupport-3.1.0.rc5/lib/active_support/dependencies.rb:223:in `load_dependency'
    from /Users/ror/projects/restaurant/highline/ruby/1.8/gems/activesupport-3.1.0.rc5/lib/active_support/dependencies.rb:639:in `new_constants_in'
    from /Users/ror/projects/restaurant/highline/ruby/1.8/gems/activesupport-3.1.0.rc5/lib/active_support/dependencies.rb:223:in `load_dependency'
    from /Users/ror/projects/restaurant/highline/ruby/1.8/gems/activesupport-3.1.0.rc5/lib/active_support/dependencies.rb:237:in `require'
    from /Users/ror/projects/restaurant/vendor/plugins/clickstream/init.rb:2
    from /Users/ror/projects/restaurant/highline/ruby/1.8/gems/railties-3.1.0.rc5/lib/rails/plugin.rb:80
    from /Users/ror/projects/restaurant/highline/ruby/1.8/gems/railties-3.1.0.rc5/lib/rails/initializable.rb:25:in `instance_exec'
    from /Users/ror/projects/restaurant/highline/ruby/1.8/gems/railties-3.1.0.rc5/lib/rails/initializable.rb:25:in `run'
    from /Users/ror/projects/restaurant/highline/ruby/1.8/gems/railties-3.1.0.rc5/lib/rails/initializable.rb:50:in `run_initializers'
    from /Users/ror/projects/restaurant/highline/ruby/1.8/gems/railties-3.1.0.rc5/lib/rails/initializable.rb:49:in `each'
    from /Users/ror/projects/restaurant/highline/ruby/1.8/gems/railties-3.1.0.rc5/lib/rails/initializable.rb:49:in `run_initializers'
    from /Users/ror/projects/restaurant/highline/ruby/1.8/gems/railties-3.1.0.rc5/lib/rails/application.rb:92:in `initialize!'
    from /Users/ror/projects/restaurant/highline/ruby/1.8/gems/railties-3.1.0.rc5/lib/rails/railtie/configurable.rb:30:in `send'
    from /Users/ror/projects/restaurant/highline/ruby/1.8/gems/railties-3.1.0.rc5/lib/rails/railtie/configurable.rb:30:in `method_missing'
    from /Users/ror/projects/restaurant/config/environment.rb:6
    from /Users/ror/projects/restaurant/highline/ruby/1.8/gems/activesupport-3.1.0.rc5/lib/active_support/dependencies.rb:237:in `require'
    from /Users/ror/projects/restaurant/highline/ruby/1.8/gems/activesupport-3.1.0.rc5/lib/active_support/dependencies.rb:237:in `require'
    from /Users/ror/projects/restaurant/highline/ruby/1.8/gems/activesupport-3.1.0.rc5/lib/active_support/dependencies.rb:223:in `load_dependency'
    from /Users/ror/projects/restaurant/highline/ruby/1.8/gems/activesupport-3.1.0.rc5/lib/active_support/dependencies.rb:639:in `new_constants_in'
    from /Users/ror/projects/restaurant/highline/ruby/1.8/gems/activesupport-3.1.0.rc5/lib/active_support/dependencies.rb:223:in `load_dependency'
    from /Users/ror/projects/restaurant/highline/ruby/1.8/gems/activesupport-3.1.0.rc5/lib/active_support/dependencies.rb:237:in `require'
    from /Users/ror/projects/restaurant/config.ru:4
    from /Users/ror/projects/restaurant/highline/ruby/1.8/gems/rack-1.3.2/lib/rack/builder.rb:51:in `instance_eval'
    from /Users/ror/projects/restaurant/highline/ruby/1.8/gems/rack-1.3.2/lib/rack/builder.rb:51:in `initialize'
    from /Users/ror/projects/restaurant/config.ru:1:in `new'
    from /Users/ror/projects/restaurant/config.ru:1 

 Solution to fix the problem:
Change 'named_scope' to 'scope' in line no - 8 in
vendor/plugins/clickstream/lib/app/models/steam.rb

Getting error "Error running 'make ', please read /Users/ror/.rvm/log/ruby-1.8.7-p352/make.log"

Installed rvm, ruby 1.9.2 through rvm successfully on Mac.
But while installing ruby-1.8.7-p352 through rvm, gives following error:

-:restaurant ror$ rvm 1.8.7
WARN: ruby ruby-1.8.7-p352 is not installed.
To install do: 'rvm install ruby-1.8.7-p352'
-:restaurant ror$ rvm install ruby-1.8.7-p352
Installing Ruby from source to: /Users/ror/.rvm/rubies/ruby-1.8.7-p352, this may take a while depending on your cpu(s)...

ruby-1.8.7-p352 - #fetching
ruby-1.8.7-p352 - #extracted to /Users/ror/.rvm/src/ruby-1.8.7-p352 (already extracted)
ruby-1.8.7-p352 - #configuring
ruby-1.8.7-p352 - #compiling
ERROR: Error running 'make ', please read /Users/ror/.rvm/log/ruby-1.8.7-p352/make.log
ERROR: There has been an error while running make. Halting the installation.


Solution to fix the problem:

Try following command:
- rvm get head;
- rvm reload;
- rvm remove 1.8.7;
- rvm install 1.8.7

Change RAILS_ROOT to Rails.root when upgrading to Rails 3

Getting following error while upgrading to Rails 3.1.0.rc5

NameError (uninitialized constant Search::RAILS_ROOT):
  app/models/search.rb:159:in `run'
  app/controllers/searches_controller.rb:66:in `new'

  Rendered highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/actionpack/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.2ms)
  Rendered highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/actionpack/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.9ms)
  Rendered highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/actionpack/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (35.6ms)

Solution to fix the problem:

Change RAILS_ROOT to Rails.root

Getting error "`const_missing': uninitialized constant ActiveRecord::Associations::AssociationCollection (NameError)"

-:restaurant ror$ rails s
/Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/thinking-sphinx-ea7673a0188f/lib/thinking_sphinx/search.rb:623: warning: encoding option is ignored - u
=> Booting WEBrick
=> Rails 3.2.0.beta application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/rake-0.9.2/lib/rake/ext/module.rb:36:in `const_missing': uninitialized constant ActiveRecord::Associations::AssociationCollection (NameError)
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/will_paginate-3.0.pre2/lib/will_paginate/finders/active_record.rb:36:in `enable!'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/will_paginate-3.0.pre2/lib/will_paginate/railtie.rb:9:in `block in <class:Railtie>'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/railties/lib/rails/initializable.rb:25:in `instance_exec'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/railties/lib/rails/initializable.rb:25:in `run'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/railties/lib/rails/initializable.rb:50:in `block in run_initializers'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/railties/lib/rails/initializable.rb:49:in `each'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/railties/lib/rails/initializable.rb:49:in `run_initializers'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/railties/lib/rails/application.rb:92:in `initialize!'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/railties/lib/rails/railtie/configurable.rb:30:in `method_missing'
    from /Users/ror/projects/restaurant/config/environment.rb:6:in `<top (required)>'
    from /Users/ror/projects/restaurant/config.ru:4:in `block in <main>'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/rack-1.3.2/lib/rack/builder.rb:51:in `instance_eval'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/rack-1.3.2/lib/rack/builder.rb:51:in `initialize'
    from /Users/ror/projects/restaurant/config.ru:1:in `new'
    from /Users/ror/projects/restaurant/config.ru:1:in `<main>'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/rack-1.3.2/lib/rack/builder.rb:40:in `eval'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/rack-1.3.2/lib/rack/builder.rb:40:in `parse_file'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/rack-1.3.2/lib/rack/server.rb:200:in `app'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/railties/lib/rails/commands/server.rb:46:in `app'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/rack-1.3.2/lib/rack/server.rb:301:in `wrapped_app'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/rack-1.3.2/lib/rack/server.rb:252:in `start'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/railties/lib/rails/commands/server.rb:70:in `start'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/railties/lib/rails/commands.rb:54:in `block in <top (required)>'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/railties/lib/rails/commands.rb:49:in `tap'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/railties/lib/rails/commands.rb:49:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

Solution to fix the problem:

In my gemfile, I was using "will_paginate-3.0.pre2"

Fixed by changing will paginate version to pre4
gem 'will_paginate', '3.0.pre4'

Sunday, July 24, 2011

Getting error "ThinkingSphinx::SphinxError (unknown local index '' in search request):"


We define index for Sphinx in model in blocks as such:
define_index do
      indexes :name, :sortable => true
      indexes description
...
...
    end

Whenever we do some changes in such type of index and start ThinkingSphinx it gives us error as :
ThinkingSphinx::SphinxError (unknown local index '<delta_name>' in search request):

To solve such type of issues, run
rake ts:rebuild
It will first do changes in config file and then will do indexing as per the changes done, then you will be able to run Thinking Sphinx.

Wednesday, July 6, 2011

searchd giving error "dyld: Library not loaded: libmysqlclient.18.dylib" on Mac

On Mac system, in one of my application using Thinking Sphinx. After installation while running searchd, it gives following errors:

$ searchd
dyld: Library not loaded: libmysqlclient.18.dylib
  Referenced from: /usr/local/bin/searchd
  Reason: image not found
Trace/BPT trap
-:sphinx-0.9.8 ror$ cd ..
-:Downloads ror$ l
-bash: l: command not found

Solution:
It's because unable to find the libmysqlclient library. Add the following to to your ~/.profile:

export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/
and it will work fine

Monday, June 27, 2011

Different types of joins in SQL

Inner Join - Inner join creates a new result table by combining column values of two tables (A and B) based upon the join-predicate. The query compares each row of A with each row of B to find all pairs of rows which satisfy the join-predicate. When the join-predicate is satisfied, column values for each matched pair of rows of A and B are combined into a result row. The result of the join can be defined as the outcome of first taking the Cartesian product (or Cross join) of all records in the tables (combining every record in table A with every record in table B)—then return all records which satisfy the join predicate

Inner joins is further classified as equi-joins, as natural joins, or as cross-joins.

Equi-join
An equi-join, also known as an equijoin, is a specific type of comparator-based join, or theta join, that uses only equality comparisons in the join-predicate. Using other comparison operators (such as <) disqualifies a join as an equi-join


Natural join
A natural join offers a further specialization of equi-joins. The join predicate arises implicitly by comparing all columns in both tables that have the same column-names in the joined tables. The resulting joined table contains only one column for each pair of equally-named columns.

Cross join
CROSS JOIN returns the Cartesian product of rows from tables in the join. In other words, it will produce rows which combine each row from the first table with each row from the second table

Outer Join
An outer join does not require each record in the two joined tables to have a matching record. The joined table retains each record—even if no other matching record exists. Outer joins subdivide further into left outer joins, right outer joins, and full outer joins, depending on which table(s) one retains the rows from (left, right, or both).

Left outer join
The result of a left outer join (or simply left join) for table A and B always contains all records of the "left" table (A), even if the join-condition does not find any matching record in the "right" table (B). This means that if the ON clause matches 0 (zero) records in B, the join will still return a row in the result—but with NULL in each column from B. This means that a left outer join returns all the values from the left table, plus matched values from the right table (or NULL in case of no matching join predicate). If the right table returns one row and the left table returns more than one matching row for it, the values in the right table will be repeated for each distinct row on the left table.

Right outer join
A right outer join (or right join) closely resembles a left outer join, except with the treatment of the tables reversed. Every row from the "right" table (B) will appear in the joined table at least once. If no matching row from the "left" table (A) exists, NULL will appear in columns from A for those records that have no match in B. A right outer join returns all the values from the right table and matched values from the left table (NULL in case of no matching join predicate). Result of right outer joins can also be produced with left outer joins by switching the table order

Full outer join
A full outer join combines the effect of applying both left and right outer joins. Where records in the FULL OUTER JOINed tables do not match, the result set will have NULL values for every column of the table that lacks a matching row. For those records that do match, a single row will be produced in the result set (containing fields populated from both tables).

Self-join
A self-join is joining a table to itself.


Refer to http://en.wikipedia.org/wiki/Join_%28SQL%29 for detailed understanding with examples

Wednesday, June 22, 2011

incompatible character encodings: ASCII-8BIT and UTF-8 in Ruby 1.9.2

While migrating from Ruby 1.8.7 to Ruby 1.9.2 getting error:
incompatible character encodings: ASCII-8BIT and UTF-8 in Ruby 1.9.2

While running rake task, getting error:

rake aborted!
C:/projects/restaurant/lib/tasks/setup.rake:158: invalid multibyte char (US-ASCII)
C:/projects/restaurant/lib/tasks/setup.rake:158: invalid multibyte char (US-ASCII)
C:/projects/restaurant/lib/tasks/setup.rake:158: syntax error, unexpected $end, expecting ')'
     Cuisine.create(:name=>"Caf⌐")
                                 ^
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:235:in `load'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:235:in `block in load'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:225:in `block in load_dependency'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:596:in `new_constants_in'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:225:in `load_dependency'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:235:in `load'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.5/lib/rails/engine.rb:131:in `block in load_tasks'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.5/lib/rails/engine.rb:131:in `each'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.5/lib/rails/engine.rb:131:in `load_tasks'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.5/lib/rails/application.rb:141:in `load_tasks'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.5/lib/rails/application.rb:77:in `method_missing'
C:/projects/restaurant/Rakefile:7:in `<top (required)>'
C:/Ruby192/lib/ruby/1.9.1/rake.rb:2373:in `load'
C:/Ruby192/lib/ruby/1.9.1/rake.rb:2373:in `raw_load_rakefile'
C:/Ruby192/lib/ruby/1.9.1/rake.rb:2007:in `block in load_rakefile'
C:/Ruby192/lib/ruby/1.9.1/rake.rb:2058:in `standard_exception_handling'
C:/Ruby192/lib/ruby/1.9.1/rake.rb:2006:in `load_rakefile'
C:/Ruby192/lib/ruby/1.9.1/rake.rb:1991:in `run'
C:/Ruby192/bin/rake:31:in `<main>'

At some other places getting error :
invalid multibyte char (US-ASCII)


Solution:
Solved it by following steps:
  • Make sure 'config.encoding = "utf-8"' is there in application.rb file.
  • Make sure you are using 'mysql2' gem
  • Putting '# encoding: utf-8' on top of rake file.
  • Above 'Starter::Application.initialize!' line in environment.rb file, add following two lines:
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8