What Changed in Rails v2.3.6 – finally we got inverse_of

RubyOnRails 15 八月 2010 | View Comments

In this commit we found we big improvement.

They add :inverse_of for active_record association

What is that used for?

Here is the example from commit message

class Man < ActiveRecord::Base
  has_one :face, :inverse_of => :man
end

class Face < ActiveRecord::Base
  belongs_to :man, :inverse_of => :face
end

m = Man.first
f = m.face

If we don’t have :inverse_of, f.man will be different object, not m@ object and what's more active_record will fire another sql query if you can @f.man.

After we add :inverse_of option, @ m == f.man , and @f.man will not fire a sql query.

But now :inverse_of only works for has_one, has_many. It also supplies inverse support for belongs_to
associations where the inverse is a has_one and it’s not a polymorphic.

I think DataMapper need change their website why DataMapper. Active Record has Identity Map now

Thanks h-lame for giving us this very useful feature which also have sufficient test.

Tagged in , , ,

What Changed in Rails v2.3.6 – redirect_to with flash

RubyOnRails 15 八月 2010 | View Comments

In this commit rails team improved redirect_to method, we can passing flash option directly.

# ... in an action

#old style
flash[:notice] = "Pay attention to the road"
redirect_to post_url(@post)

#new way
redirect_to post_url(@post), :notice => "Pay attention to the road"

They also add some convenient methods for flash object. Now you can get/set flash object in view by alert,alert=(message),notice,notice=(message)

Tagged in , , ,

RVM new feature auto-switch gemset – project level gemset

RubyOnRails, Tips 15 八月 2010 | View Comments

Today I noticed RVM have project level gemset

It’s easy to use. Just put a .rvmrc into the folder you want to switch ruby or gemset

Like to we have a rails3 project we can put a .rvmrc into project folder. insert following content in.

rvm 1.8.7@rails3

It’s just like we run rvm use 1.9.7@rails3

The detail usage you can get from RVM Website

Tagged in , ,

What Changed in Rails v2.3.6 – Add Column With Positioning

RubyOnRails 31 七月 2010 | View Comments

In this commit Rails add a new feature that we can specify what’s the postillion to add a column.

# In migration

# add column to in first position
add_column :testings, :new_col, :integer, :first => true

# add column after another column
add_column :testings, :new_col, :integer, :after => "column_a"

# change a column with position
change_column :testings, :second, :integer, :first => true
change_column :testings, :second, :integer, :after => "column_c"

About how column position affect performance, you can see this blog post

Thanks bmarini commit this feature, also thanks jeremy reviewed this commit.

Tagged in ,

Clean Mongodb in UnitTest, Rspec, Cucumber using Mongoid

RubyOnRails, Tips 21 七月 2010 | View Comments

Rails UnitTest



in file test/test_helper.rb in class ActiveSupport::TestCase add

  teardown :clean_mongodb
  def clean_mongodb
    puts "cleaning mongodb...."
    Mongoid.database.collections.each do |collection|
      unless collection.name =~ /^system\./
        collection.remove
      end
    end
    puts "finished cleaning mongodb."
  end

Rspec



in file spec/spec_helper.rb, in Spec::Runner.configure add

config.after(:each) do
    puts "cleaning mongodb...."
    Mongoid.database.collections.each do |collection|
      unless collection.name =~ /^system\./
        collection.remove
      end
    end
    puts "finished cleaning mongodb."
end

Cucumber



add new file features/support/mongodb_clean.rb

After do |scenario|
  puts "cleaning mongodb...."
  Mongoid.database.collections.each do |collection|
    unless collection.name =~ /^system\./
      collection.remove
    end
  end
  puts "finished cleaning mongodb."
end

Tagged in , , , ,

Manage Gems Separately in Different Projects Use Bundler

RubyOnRails 3 七月 2010 | View Comments

Before we use bundler, we unpack all dependencies into vendor folder. But we found it hard to use, especially one of your gem require rubygems in code, yes it happens.

The killer feature bunlder has in my view is: you can install gems in any folder, that’s means you can install gems into your project folder directly. So each of your projects will has it’s own gemset.I think this is a better solution than use rvm to create separate gemset for each project(see my previous entry).

If you have already installed bundler, or you have already using bundler, you can skip the first part, go bundler for passenger directly

Upgrade Your Gem

First, check whether your gem version >= 1.3.6, if not upgrade your gem

  • For mac user, just gem update --system
  • For linux user:
    1. Install rubygems-update gem gem install rubygems-update
    2. Upgrade rubygem, run ./bin/update_rubygems, if it says: “can’t find command”, you can go to your gem EXECUTABLE DIRECTORY which you can get by run command gem environment

Install bundle

Install bundler using gem install bundle, as this blog wrote, bundler verison is 0.9.26

Manage your gems use bundle

There are good documents on bundler website, you can check the basic usage there. I’ll not mention here.

ok, let’s do it.

bundle install .bundle

Yes, that’s it. Now your gems will only live in your project folder. It’s very sweet when you need deploy multi projects on one machine.

Bundler with passenger

After you deploy your bundler enhanced project with passenger, passenger will yelling: please gem install bundler.
the solution is add gem dependencies in Gemfile. Yes it’s strange, but I google about it, I haven’t better solution, if you know you can tell me.

Tagged in , , , ,

Tips – How Many Argument a Method Has

RubyOnRails 10 六月 2010 | View Comments

Find a method in arity

Here is doc from ruby doc

Returns an indication of the number of arguments accepted by a method. Returns a nonnegative integer for methods that take a fixed number of arguments. For Ruby methods that take a variable number of arguments, returns -n-1, where n is the number of required arguments. For methods written in C, returns -1 if the call takes a variable number of arguments.

  class C
     def one;    end
     def two(a); end
     def three(*a);  end
     def four(a, b); end
     def five(a, b, *c);    end
     def six(a, b, *c, &d); end
   end
   c = C.new
   c.method(:one).arity     #=> 0
   c.method(:two).arity     #=> 1
   c.method(:three).arity   #=> -1
   c.method(:four).arity    #=> 2
   c.method(:five).arity    #=> -3
   c.method(:six).arity     #=> -3

   "cat".method(:size).arity      #=> 0
   "cat".method(:replace).arity   #=> 1
   "cat".method(:squeeze).arity   #=> -1
   "cat".method(:count).arity     #=> -1

Tagged in ,

Tips – Set Default Value for A Hash

RubyOnRails, Tips 10 六月 2010 | View Comments

Can hash have a default value, when it doesn’t have a key?

Answer is yes!

h = Hash.new("default")
h["wrong_value"] # => "default"

Tagged in , ,

Manage Your Gem Using RVM Gemset

RubyOnRails, Tips 5 六月 2010 | View Comments

I think you already heard of RVM, a great tool for ruby version management.

What I like most is Gemset, It allow you manage you gems in separate namespace.

First, Install RVM. The official website have great document

Then Install ruby, I’ll install ruby 1.8.7

rvm install 1.8.7

After you installed ruby, you will have default gemset and global gemset
List all you gemset

rvm gemset list

Create new gemset

rvm gemset create new_gemset

Use your newly created gemset

rvm gemset use new_gemset

You can also set your newly created gemset as default

rvm use 1.8.7@new_gemset --default

Here you may ask, do I need install all basic gem like ruby-debug for every gemset?

RVM has already solved your problem.

First, switch to your global gemset.

rvm use 1.8.7@global

Then Install gems which you think it is default gem

Then, you can switch back to your gemset.

You may think, I may installed some gems twice, here you can uninstall the gem you have already installed in global gemset.

Tagged in , ,

Tips – Print test name and file name when testing (Final Solution)

RubyOnRails, Tips 13 五月 2010 | View Comments

As previous solution doesn’t works, after read source code of ActiveSupport::TestCase I find a final solution.

class ActiveSupport::TestCase
  setup :log_test
  private
  def log_test
    unless @already_logged_this_test  
      puts "\n\nStarting #{@method_name} in class:#{self.class.name}\n#{'-' * (20 + @method_name.length + self.class.name.length)}\n"  
    end  
    @already_logged_this_test = true

  end

setup is a callback of ActiveSupport::TestCase. So we doesn’t hack anything. It works fine.

And what’s more rake test already support verbose model

  rake test:units TESTOPTS="-v"

if you using run test directly like this, TESTOPTS="-v" doesn’t work

ruby -Itest test/units/some_test.rb

Tagged in , ,

Ads Plugin created by Jake Ruston's Wordpress Plugins - Powered by and football database.