Tatva-Artha

meaning of "it"

Archive for the ‘rails’ tag

Upgrading to ruby 1.9: rbx-require-relative requires Ruby version ~> 1.8.7

without comments

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.

Written by Sharad

August 21st, 2011 at 1:55 pm

Posted in All,Technology

Tagged with , , ,

Deploy at will!

without comments

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.

Written by Sharad

April 6th, 2011 at 9:03 pm

Posted in All

Tagged with , , ,

Mocha unexpected invocation errors

without comments

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'

Written by Sharad

March 11th, 2011 at 10:38 pm

Posted in All,Technology

Tagged with , ,

ActiveRecord::MissingAttributeError: missing attribute — a bug or a feature?

with one comment

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.

Written by Sharad

March 3rd, 2011 at 8:07 pm

Posted in All,Technology

Tagged with , ,

Installing mysql gem with bundler on Snow Leopard

with 2 comments

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.

Read the rest of this entry »

Written by Sharad

October 7th, 2010 at 6:34 pm

Posted in All

Tagged with , , , , ,

Rails Constants: Location matters

without comments

Rails framework provides a standard location for defining application specific constants. All default constants go in constant/environment.rb and any environment specific overrides go in config/environments/<env>.rb file. Normally, any constant that is overridden in environment specific file takes precedence. However, there is a catch. If a constant is defined outside of Rails::Initializer.run block then it overrides all other definition preceding it.

Hence, when defining constants in default config/environment.rb file, beware:

require File.join(File.dirname(__FILE__), 'boot')
Rails::Initializer.run do |config|
A_CONSTANT = "value"
end
# Do not define environment specific constants here.

Enjoy!

Written by admin

June 1st, 2009 at 9:42 pm

Posted in All,Technology

Tagged with ,

ActionMailer, GMail and TLS

with one comment

Prior to rails 2.3.2, if you needed to use GMail as your SMTP carrier (sending out emails from your app), you had to use one of the following plugins to modify the ActionMailer::Base class to support TLS for ActiveRecord.

  • tlsmail
  • ambethia-smtp-tls
  • openrain-action_mailer_tls
  • simplificator-tls-support

In rails 2.3.2, this is supported natively, provided you had proper configuration specified:

config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:enable_starttls_auto => true,
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'localhost.localdomain',
:authentication => :plain,
:user_name => "username",
:password => "password"
}

This should work without any plugins/gems in rails 2.3.2. The only caveat is that ruby version has to be >= 1.8.7. It won’t work with ruby 1.8.6 or older, as documented here.

Enjoy!

Written by admin

May 4th, 2009 at 9:23 pm

Posted in All,Technology

Tagged with ,

Installing passenger/mod_rails on CentOS

without comments

Installing passenger/mod_rails on CentOS sometimes is not as simple as:


$ gem install passenger
and
$ passenger-install-apache2-module

In my case, I also had to install “apr-util-devel” yup package. Also, for some reason the “sudo yum install ” didn’t work for me. I actually had to become root and then perform install.


$ sudo su -
$ yum install apr-util-devel

After this, “gem install passenger” and passenger-apache module install worked fine.

Written by admin

May 4th, 2009 at 9:16 pm

Posted in All,Technology

Tagged with ,

Installing mysql gem on CentOS

without comments

Installing mysql gem on cent-os may sometimes be not as easy as:

$ gem install mysql

It may result in compilation error and fail to install. While this may happen for various reasons, in my case, I had to install “mysql-devel” package.

$ sudo yum install mysql-devel

Once I did this, the mysql gem installed happily.

Written by admin

May 4th, 2009 at 4:26 pm

Posted in All,Technology

Tagged with ,