exceptionz

Thoughts on Technology, Methodology and Programming.

Archive for the ‘Rails’ Category

How to fix the iconv require error in Ruby 1.9

Posted by Marcus Wyatt on 3 February 2010

So you’re trying out the pre-release version of Rails 3.0 with Ruby 1.9 and you keep on getting the following error:

activesupport-3.0.pre/lib/active_support/inflector/transliterate.rb:2:in `require’: no such file to load – iconv (LoadError)

If you are using rvm to install and switch between different versions of ruby then follow the following steps:

  1. Install readline using rvm: rvm install readline rvm package install readline
  2. Now install iconv by executing: rvm install iconv rvm package install iconv
  3. if you already have a version of Ruby 1.9 installed, we need to remove it by executing: rvm remove 1.9.1/1.9.2
  4. The final step is to re-install the version of ruby: rvm install –trace 1.9.2 -C –with-iconv-dir=$HOME/.rvm/us
    (If you are having problems with the command, use the pastie that Glenn Rempe posted in the comments. Thanks Glenn)

Now to test that it worked:

  1. Change to the ruby version you installed: rvm use 1.9.1/1.9.2
  2. Check you are on the right version of ruby: ruby -v #=> ruby 1.9.1p378 (2010-01-10 revision 26273) [i386-darwin10.2.0]
  3. Start a new irb session
  4. Now you should be able to require ‘iconv’ and get a ‘true’ result

    irb(main):001:0> require ‘iconv’

Now you should be good to go!


Advertisements

Posted in Rails, Ruby | Tagged: , , | 9 Comments »

Execute sql within rails environment without using your models

Posted by Marcus Wyatt on 19 August 2008

When you have a query that you need to run as raw sql against the database, like batch data processing or maybe some data cleanup, and you don`t want to create ActiveRecord models to handle the data. Because creating models is expensive and slow. So we want to execute directly against the database. But how do we execute directly on the database from within our rails application environment? The solution is to use ActiveRecord::Base.connection to do the work:

sql = "my complex sql statement"
	ActiveRecord::Base.connection.execute(sql)

This is really nice way to execute queries within rake tasks. In the following example I show how you would establish a database connection as well:

namespace :db do
	
	  desc "Cleanup the database by setting rows to deleted when older than xxx. Defaults to development database.  Set RAILS_ENV=[production, test, etc.] to override."
	  task :cleanup => :environment do
	    sql = <<-SQL
	      -- do some cleanup code
	    SQL
	    # used to connect active record to the database
	    ActiveRecord::Base.establish_connection
	    ActiveRecord::Base.connection.execute(sql)
	  end

	end
	

Here is a very funny tutorial on rake. That`s all for now folks…

Technorati Tags: , ,

Post created with TextMate.

Posted in Rails | 5 Comments »

Run gem install behind a firewall in Windows

Posted by Marcus Wyatt on 18 March 2008

I’m contracting at a client running a Novell network and wanted to create some small little ruby scripts to automate some tasks for me. But I ran into problems using gem install behind their firewall. I’m getting the following error:

ERROR: While executing gem … (Gem::RemoteSourceException) HTTP Response 407

Here is the steps I followed to get gem install working on my windows xp machine in painstaking detail: 🙂

Firstly we need to set the HTTP_PROXY environment variable. I’ve played around with different options, using uid and pwd and other suggestions, but all I needed was the following:

  1. On the desktop right click the ‘My Computer’ icon and select properties.
  2. Now you can either add the HTTP_PROXY variable to the ‘User’ or the ‘System’ variables by clicking the ‘New’ button.
  3. Within the new system variable dialog, specify ’HTTP_PROXY’ as the variable name and in the value area a url in the following format – http://%5Bproxy_ip%5D:%5Bproxy_port%5D – i.e. http://127.168.1.3:5865

Next we install the Ruby Win32 SSPI which gives ruby the ability to authenticate with ISA natively. We need this library because as far as I understand, it patches the Open-uri library because of some incompatibilities with windows. Anyways, follow the following steps to get it working:

  1. Download rubysspi from the Ruby Win32 SSPI project page(also available as gem install rubysspi but that doesn’t help much, does it?)
  2. Install the gem locally using the following command: gem install [local_path_to_gem]\rubysspi-1.2.3.gem i.e. C:\gems\rubysspi-1.2.3.gem
  3. Now copy the spa.rb file from the gem install directory and paste it in the site-ruby directory. i.e. If ruby is installed in C:\ruby, then the paths should be:
    • origin path – C:\ruby\lib\ruby\gems\1.8\gems\rubysspi-1.2.3\spa.rb
    • destination path – C:\ruby\lib\ruby\site_ruby\spa.rb
  4. Find gem.bat in your ruby bin directory (C:\ruby\bin) and replace the last line (i.e. "%~d0%~p0ruby" -x "%~f0" %*) with the following line – @"ruby" -rspa "c:\ruby\bin\gem" %1 %2 %3 %4 %5 %6 %7 %8 %9
  5. Test you can access the remote ruby gems by executing the following command: gem list rails -r
  6. You should see similar output as shown above.

Now we can work again without any issues when trying to install or update gems.

Enjoy!!!

Technorati Tags: , ,

Posted in Development, Rails, Ruby, Software, Tools, XP | 36 Comments »

TextMate command to annotate your current ActiveRecord Model with the DB Schema

Posted by Marcus Wyatt on 18 March 2008

Edit: If you can’t wait until the end, here is the download link.

Who hasn’t used the Annotate Models plugin written by Dave Thomas, of Rails Pragmatic Studio fame? If you haven’t don’t fret… The plugin basically adds a comment block to the top of all of your model classes documenting the current database schema for the given model. I like to have the info in my model files to make it a little easier to work with models.

Annotated Models Plugin

The rails bundle within TextMate gives you a ‘Show DB Schema for current class’ command you can press to show a tool tip with the database schema (Control + Shift + Command + S). This works great, but I find the command sequence to long and the slight wait to see the schema info breaks my rhythm.

Show DB Schema for Current Class

To solve the problem I made a copy of the source files used by the command and modified it slightly to output the info as comments at the top of the file. Now I have nicely annotated model files. This is a real time saver.

Annotated Models Bundles

The bundle also contain the the Beautify command. This command adds automatic code formatting capability for your ruby code. I found the beautify command from Tim Burks. Cheers Tim…

You can download the bundle here.

Technorati Tags: , , , ,

Blogged from TextMate

Posted in Mac, OS X, Rails, Ruby, Software, TextMate | Leave a Comment »

Ruby, Rails, TextMate, JavaScript, Mac OSX, Subversion

Posted by Marcus Wyatt on 15 February 2008

Ruby

Rails

TextMate

JavaScript

Mac OSX

Subversion

Technorati Tags: , , , , ,

Posted in JavaScript, Mac, OS X, Rails, Ruby, Software, Subversion, TextMate | Leave a Comment »

Ruby, Rails, RSpec, JavaScript, Ubuntu, Mac OS X

Posted by Marcus Wyatt on 12 February 2008

Ruby

Rails

RSpec

JavaScript

Ubuntu

Mac OS X

Technorati Tags: , , , , ,

Powered by ScribeFire.

Posted in BDD, JavaScript, Mac, OS X, Rails, RSpec, Ruby, Ubuntu | 1 Comment »

Ruby, Rails, RSpec, Rails Plugin, JavaScript, OS X

Posted by Marcus Wyatt on 29 January 2008

Ruby

Rails

RSpec

Rails Plugin

JavaScript

OS X

Technorati Tags: , , , , ,

Post created with TextMate.

Posted in JavaScript, OS X, Rails, Rails Plugins, RSpec, Ruby | Leave a Comment »

Ruby, Rails, Rails Plugin, CSS

Posted by Marcus Wyatt on 20 January 2008

Ruby

Rails

Rails Plugin

  • AttributeFu – makes multi-model forms so easy you’ll be telling all of your friends about it.
  • assert_efficient_sql – the assertion intercepts and copies out your MySQL SELECT statements, then calls EXPLAIN on each one, and inspects the results for common problems.

CSS

SEO

Technorati Tags: , , , ,

Post created with TextMate.

Posted in CSS, Rails, Rails Plugins, Ruby, SEO | Leave a Comment »

Ruby, Rails, OS X (Leopard), CSS

Posted by Marcus Wyatt on 14 January 2008

Ruby

  • Hash#tomod – extend your class to except a hash of values in the constructor and then access the values as if they are read accessors on the object. Very enlightening post, I’m sure ActiveRecord::Base implements something.

Rails

OS X (Leopard)

CSS

Technorati Tags:

,

,

,

Powered by ScribeFire.


Posted in CSS, Mac, OS X, Rails, Ruby | Leave a Comment »

Ruby, Rails, JavaScript

Posted by Marcus Wyatt on 8 January 2008

Ruby

Rails

JavaScript

Technorati Tags: , ,

Post created with TextMate.

Posted in JavaScript, Rails, Ruby | 1 Comment »