Posts Tagged ‘ruby’

Fixing the “You have to install development tools first” error with Nokogiri.

Friday, November 26th, 2010

After wracking my brains around a strange issue installing Nokogiri, wmoxam in #rubyonrails and I figured out how to get it to install.

Problem: You go to install nokogiri, either with bundler or with a straight “gem install nokogiri”, and you get an error like the following:

In your wisdom, you decide to jump over to Aaron Paterson’s Installing Nokogiri page, where he tells you to install the libs, libxml2 and libxslt, manually and configure the gem install to those libs with flags … but it still doesn’t work.

Solution: You are probably using RVM, right? Yeah, we’ll when you installed ruby 1.9.* with it, it was probably old. Update your rvm install and re-install ruby with the new RVM version, and you will be green.

1
2
3
4
5
6
7
8
rvm update head
rvm --force install 1.9.2
gem install bundle

## in your rails app
bundle
Installing nokogiri (1.4.2) with native extensions
 ...

Boom!

Tags: , ,
Posted in Information | 12 Comments »

Adding Methods on Associations in Rails

Wednesday, February 17th, 2010

Many times, you may find yourself interested in moving some of your logic outside of the view and into a model, so that you can re-use it throughout your app. But sometimes, the method you may want to define needs to be on an association of two models, and not on the models themselves.

As a real world scenario, imagine you are making an online game in which two models exist, Game and Score. The Game model keeps track of when the game was started, who started it, the level of difficulty, etc. The Score model keeps track of the value of the score, the round the score was done in, and the time the score was placed. The Game model has many Score models. Obviously, when the game is over, we are going to want to be able to see the total score. But what model do we define the method on? The Game model, itself, doesn’t have any scores and a Score object only knows about itself and not all the others that belong to the parent Game object.

This is a situation where the method needs to be defined on the association of Game and Score. Anytime the value you are trying to obtain can only be calculated with a parent and a child of an association, then you need to define the method on the association.

How to define it

Turns out that defining the method is a little trivial. The has_many method, used for creating one-to-many relationships, accepts a block in which you can define methods. Inside that block, self is set to the collection of children in the relationship. So now to define your total_score method:

1
2
3
4
5
6
7
8
9
  class Game < ActiveRecord::Base
    has_many :scores do
      def total_score
          total = 0
          self.each { |score| total += score.value }
          total
      end
    end
  end

Then, in the app …

1
2
3
4
5
6
7
g = Game.create(:user_id => 1)
g.scores.build(:value=>20)
g.scores.build(:value=>40)
g.scores.build(:value=>60)
g.save
g.scores.total_score
# 120

Tags: , ,
Posted in Code | 2 Comments »

Ruby 1.9.1 & Textmate Don’t Play Nicely Together

Friday, January 8th, 2010

Ruby 1.9.1 has been out for a long time now, so you may feel pretty comfortable about installing it on your box and doing to the big “let’s see what this broke” game with Rails. We’ll I just wanted to give you a little heads up with another program you may have not expected issues with, Textmate.

What? The Horror! Not being able to code Ruby with Textmate. Well, fortunately, that is not entirely true. You can still use the infamous editor to edit you Ruby code, no problem. The issue comes when trying to use any of the generators or functions that require you to execute Ruby code. You may find yourself running into this puzzling error:

invalid multibyte char (US-ASCII)

For those of you not in the know, the handling of different character types have changed in Ruby 1.9 and Textmate’s latest release does not accommodate this change.

I will update this with any updates, in terms of fixed Textmate versions or any other reliable solutions.

Tags: ,
Posted in Information | No Comments »

Ruby on Rails May Become Really Popular Soon.

Saturday, December 5th, 2009

I found this and I just thought it was too adorable not to repost: http://37signals.com/svn/archives/000606.php

Tags: ,
Posted in Uncategorized | No Comments »