Tag Archives: Ruby

Setup Passenger 4 (pre-release RC4) for Apache2 and Ruby 2.0.0

Setting up Apache2 and passenger for Ruby 2.0.0 is actually pretty simple. As of right now, for Passenger to work with Apache2 and Ruby 2.0.0 you will need to install Passenger 4 RC4 or above, which currently isn’t (technically) a stable release. I have found it’s pretty stable though.

Install passenger through RubyGems

First, install passenger through gems:

gem install passenger --pre

Continue reading

Install multiple Ruby versions on Ubuntu 12.04 using RVM

RVM or Ruby Verion Management is a library written for managing multiple version of Ruby on a single machine, and even for a single user. Using this set of libraries you can easily install a version of Ruby and switch between different version as and when you see fit. This is great for developers, especially those having to maintain multiple different applications, using different Ruby and potentially rails versions.

Installation of RVM on Ubuntu 12.04 for a single user

To start, download RVM and automatically install the latest stable version of Ruby

curl -L https://get.rvm.io | bash -s stable --ruby

Here we download a file which has a series of commands for installing RVM and the latest stable version of Ruby. `-s` tells bash to read commands from the standard input, which is the downloaded file from the rvm website. This process will take a while, depending on your connection speed and processors as it downloads and compiles Ruby and Rubygems. Continue reading

Ruby (Rails) constant lookup precedence

Recently I’ve been running into an interesting problem (at least, to me). Within a web application I am working on I found what I thought was a sporadic error occurring, which resulted in the need to restart Apache in order to temporarily fix it. That was, until I made a change to the controller and thus the issue appeared again, until I restarted Apache. The error:

uninitialized constant Frontend::User::ForgottenPasswordController::User

This puzzled me for quite a while, but I continued to develop while every now and again I would come up against this and look for answers. I found nothing, but knew the following:

  • User constant (model class) exists and works fine
  • This only happened in specific controllers

Jumping onto irc.freenode.net and having a chat with `workmad3` we solved the problem, and it’s a pretty simple one, once you understand how Ruby and thus Rails references constants. Continue reading

How to: Detect if validating inside model

In a model I needed to modify a getter to append a string if a certain attribute evaluated to true. This works great, until I needed to detect the uniqueness of the attribute against a column in the DB through the :uniqueness validator.

The problem

1
2
3
4
5
6
7
8
9
  def label
  new_value = read_attribute(:label)
 
  if self.something == true
    new_label += 'Str'
  end
 
  new_label
end

When validating against the label’s uniqueness the validator will read the label attribute from my custom getter, thus, if a record in the DB has a label of ‘Test’, and our new record has a label of ‘Test’ and self.something evaluates to true, the validator will get ‘TestStr’ from the getter and compare the custom value against the DB. Based on this setup, no match would be found. Continue reading

5 Useful PHP to Ruby methods

When I first started writing in ruby I found myself needing to know the ruby method equivalents to specific PHP functions. These methods are widely used by myself for debugging and day to day checking of data types.

PHP’s die(‘message’) becomes

1
abort 'message'

PHP’s get_class_methods() becomes

1
object.methods.inspect

PHP’s in_array() becomes

1
array.include? 'item'

Continue reading