Archive for the ‘ruby’ tag
Intalling pg gem on ubuntu
Installing pg gem (for protgres) on ubuntu server requires following packages
$ sudo apt-get install libpq-dev $ sudo apt-get install postgresql-server-dev-all
Upgrading to ruby 1.9: rbx-require-relative requires Ruby version ~> 1.8.7
If you are upgrading from ruby 1.8 or Ruby Enterprise 1.8.7 to ruby 1.9.2, you may encounter this error.
Installing rbx-require-relative (0.0.5) Unfortunately, a fatal error has occurred. Please report this error to the Bundler issue tracker at https://github.com/carlhuda/ bundler/issues so that we can fix it. Thanks!/Users/sjain/.rvm/rubies/ruby-1.9.2-p290/ lib/ruby/site_ruby/1.9.1/rubygems/ installer.rb:364:in `ensure_required_ruby_version_met': rbx-require-relative requires Ruby version ~> 1.8.7. (Gem::InstallError)
This most likely happens because you are declaring a dependency on ruby-debug gem in your Gemfile.
group :development do gem 'ruby-debug' end
With ruby 1.9, you need to update this with new gem name ruby-debug19.
group :development do gem 'ruby-debug19' end
This will eliminate the dependency on rbx-require-relative and fix the issue.
Debugging Passenger Memory Issues
There was an interesting thread on ruby-passenger application server newsgroup recently: http://groups.google.com/group/phusion-passenger/browse_thread/thread/f48ad0eb018a2482
Here is my take away:
# returns [class, count] tuples for all active records in the heap def ar_space GC.start h = Hash.new(0) ObjectSpace.each_object do |o| next if o.__id__ == self.__id__ next unless ActiveRecord::Base === o h[o.class.to_s] += 1 end return h.sort{|a,b| -(a[1]<=>b[1])} end
Nifty little snippet to fetch number of active record objects on heap. I am guessing, incorporating this as Controller after_filter can help with cost of each request..
Deploy at will!
Shamelessly copying one point from a github blog. It is that good.
Deploy at Will!
At the first RailsConf I had the pleasure of hearing Martin Fowler deliver an amazing keynote. He made some apt metaphors regarding agile development that I will now paraphrase and mangle.
Imagine you’re tasked with building a computer controlled gun that can accurately hit a target about 50 meters distant. That is the only requirement. One way to do this is to build a complex machine that measures every possible variable (wind, elevation, temperature, etc.) before the shot and then takes aim and shoots. Another approach is to build a simple machine that fires rapidly and can detect where each shot hits. It then uses this information to adjust the aim of the next shot, quickly homing in on the target a little at a time.
The difference between these two approaches is to realize that bullets are cheap. By the time the former group has perfected their wind detection instrument, you’ll have finished your simple weapon and already hit the target.
In the world of web development, the target is your ideal offering, the bullets are your site deploys, and your customers provide the feedback mechanism. The first year of a web offering is a magical one. Your customers are most likely early adopters and love to see new features roll out every few weeks. If this results in a little bit of downtime, they’ll easily forgive you, as long as those features are sweet. In the early days of GitHub, we’d deploy up to ten times in one afternoon, always inching closer to that target.
Make good use of that first year, because once the big important customers start rolling in, you have to be a lot more careful about hitting one of them with a stray bullet. Later in the game, downtime and botched deploys are money lost and you have to rely more on building instruments to predict where you should aim.
Mocha unexpected invocation errors
If you find that your tests pass when run alone but fail with “unexpected invocation” inside “rake test”, you are most likely seeing behavior that is caused by mocha load order.
This is a documented gotcha with mocha. And also discussed here.
It happens when mocha gem is listed as library dependency inside rails app. The library gets loaded too soon. The fix is to load mocha after rails has booted up.
For rails < 3.0.0, this means removing -- config.gem 'mocha' -- entry from config/environment.rb and adding -- require 'mocha' -- at the bottom of test/test_helper.rb.
For rails >= 3.0.0, this means adding — :require => false — inside Gemfile entry as follows.
# Gemfile group :test do gem 'mocha', :require => false end
And then adding following line at the bottom of test/test_helper.rb:
# test_helper.rb require 'mocha'
Installing RMagick gem on Mac OSX
Commands for installing image-magick package and rmagick gem on max osx:
sudo port install tiff -macosx imagemagick +q8 +gs +wmf sudo gem install rmagick
ActiveRecord::MissingAttributeError: missing attribute — a bug or a feature?
Sooner or later, the need arises to assign default values to model attributes. In rails this is usually done in model’s after_initialize() with a new_record? guard.
Something like:
class User def after_initialize if new_record? self.country = "US" end end end
This initializes the object with proper default so the UI form gets properly populated and the database gets proper default if user hasn’t overridden it.
Sometimes, you realize that you have (pre-existing?) objects in database that also need the same default if the current value is nil. In such case, you may modify your initializer like this:
class User def after_initialize self.country ||= "US" end end
This works for both scenarios: User.new and User.find.
However, it introduces a behavior that may not be apparant immediately.
$ script/console ree> User.find(u.id) User Load (0.9ms) SELECT * FROM `users` WHERE (`users`.`id` = 1) ree> User.exists?(:id => u.id) RateSearch Load (0.6ms) SELECT `users`.id FROM `users` WHERE (`users`.`id` = 1) LIMIT 1 ActiveRecord::MissingAttributeError: missing attribute: country /[prj]/app/models/User.rb:3:in `after_initialize'
A closer look reveals that ActiveRecord tries to be smart and only fetch ‘id’ column when performing Model.exists? call.
Many people have tripped this and logged it as a bug. The report has been silently ignored, and I believe, for good/performance reasons.
So, what do we get around it? Here’s one way.
class User def after_initialize self.country ||= "US" rescue ActiveRecord::MissingAttributeError # this should only happen on Model.exists?() call. It can be safely ignored. end end
As they say, it is not a bug, it is a feature.
Online Regular Expression Tool
Rubular, is a tool that you may find handy when developing ruby regular expression for scraping unstructured data needs.
Good one!
Installing mysql gem with bundler on Snow Leopard

Between 0.9.26, RC and final 1.0 release, bundler went thru some heavy changes with respect to command line options it supports. Luckily, twitter, forum and blogs kept everybody in the loop.
The blogs, however, became obsolete quickly and even blog posts few months old don’t work with latest version of bundler. We came across this issue that kept is in a loop for a while.
Snow Leopard changed a few things with ruby and mysql gem when it came out. Most mysql installation issues are hammered out by now and are well documented.
Stick with ERB or move to Haml
Haml is gaining popularity in Rails community. It claims higher productivity compared to defacto ERB templating. Not everybody agrees though. I see 2 short-term problem with haml.
- ERB is similar to it pre-decessor and hence easier to learn. Compared to JSP etc. ERB is similar, you still see lots of HTML tag with interleaved ruby (or Java). Although verbose, it is closer to how your HTML would finally look like.
- If your team has a dedicated HTML programmer (Designer as we may call them). These folks are very good at plain HTML and don’t want the trouble of converting files and having all the plumbing around when working. It is not efficient for them.
Despite this, I see Haml as valid alternative for following reasons: