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 2.3.6, active_record, inverse_of, RubyOnRails
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 2.3.6, active_controller, flash, RubyOnRails
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.
It’s just like we run rvm use 1.9.7@rails3
The detail usage you can get from RVM Website
Tagged in gemset, ruby, rvm
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 2.3.6, RubyOnRails
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 cucumber, mongoid, rspec, RubyOnRails, test
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:
- Install rubygems-update gem
gem install rubygems-update
- 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.
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 bundler, deployment, gem, passenger, RubyOnRails
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 ruby, Tips
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 hash, ruby, Tips
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
After you installed ruby, you will have default gemset and global gemset
List all you gemset
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.
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 gemset, ruby, rvm
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 RubyOnRails, test, Tips