Technology
From the Trenches

Archive for the 'Ruby on Rails' Category

Ruby on Rails has_many :through NameError: uninitialized constant

Monday, March 10th, 2008

Are you getting the error message, “NameError: uninitialized constant” from Rails? I was too, and the solution was a simple change to my models. I have two models with “has_many :through”, for my “events” and “people” tables:


class Event < ActiveRecord::Base
  has_many :memberships
  has_many :people, :through => :memberships
end

And:


class Person < ActiveRecord::Base
  has_many :memberships
  has_many :events, :through => :memberships
end

The join table is “memberships”, and the problem was in its model:


class Membership < ActiveRecord::Base
  validates_presence_of :event_id, :person_id
  belongs_to :people
  belongs_to :events
end

It is a subtle problem; the model looks OK. However, the "belongs_to" statements should point to the models of the other two tables, not the names of the tables themselves. The models are Event and Person, so the join model should be:


class Membership < ActiveRecord::Base
  validates_presence_of :event_id, :person_id
  belongs_to :person
  belongs_to :event
end

It took me a while to see the problem because it is so subtle, even though the answer is everywhere. Hopefully spelling it out here will help someone.

Tags: ,

The MySQL ruby gem on Leopard (client)

Friday, February 8th, 2008

rubygems.png I just started getting back into Ruby on Rails, after a hiatus while I battled with Leopard server. I discovered that setting up my Rails development environment in Leopard wasn’t as perfectly straightforward as it was in Tiger.

I installed the binary distribution of MySQL, and proceeded to install the mysql ruby gem to connect to it. First, the build failed because it didn’t know where to look for the mysql client libraries, then it failed again because by default the Makefile tries building for both PPC and Intel architectures.

To make a long story short, the solution is to add an ARCHFLAGS environment variable specifiying your architecture, and to provide the path to mysql_config on the command line. For the former, add:


ARCHFLAGS="-arch i386"

To your /etc/bashrc (assuming you use the default shell, bash), and open a new terminal or run ‘bash’. If you’re on PPC architecture, change “i386″ to “ppc”. Then try the gem again with the path to mysql_config appended, like so:


sudo gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config

That is all on one line.

Tags: , , , ,

Learning Rails? Check out Rails 101

Wednesday, November 21st, 2007

rails.gif Ruby on Rails developer and teacher Peter Marklund, of Stockholm, has published slides from his five day introductory course on Ruby on Rails. It provides a great summary of many of Rails’ features and benefits, including a summary of Ruby syntax for common tasks.

Tags:

Agile Web Development

Friday, May 18th, 2007

Ruby on Rails has been a sensation for web developers on the Internet since its release a few years ago. Although the hype has somewhat died down, the project continues to thrive and attract an adoring audience. We at NetMojo are getting into Ruby, and expect to develop some Rails apps this summer.

Programmers Love Ruby on Rails

I recently came across this presentation by Gregg Pollack. He makes a great case against the traditional model of software development, which includes specification documents and contracts to meet those specs. Instead, he advocates Agile Development, which is one of the main ideas around which Ruby on Rails is created.

The video is well worth watching if you’re a web developer or someone who is considering starting a web development project. It gives an overview of what agile development is, and why it is a much better choice for web development. Given that it’s from the Rails Envy site, it has a Ruby on Rails bent, but the argument for agile development applies to any language.

Tags: , ,