13 January 2010 ~ 0 Comments

Installing Ruby on Rails 2.3+ plugins from github

I’ve been banging my head against this wall for quite awhile now, and I just finally figured out the answer. Like I’ve done in other posts, I’ll just post what worked for me, and hopefully it’ll help other people.

I’m running Ruby 1.9 and Ruby on Rails 2.3.3 on Snow Leopard. I’ve been trying to install plugins (specifically, Authlogic and Carmen) for a couple days now using the following two commands (as taken from the main github pages):

script/plugin install git://github.com/binarylogic/authlogic.git
script/plugin install git://github.com/jim/carmen.git

In return, I received the following errors:

Plugin not found: ["git://github.com/binarylogic/authlogic.git"]
Plugin not found: ["git://github.com/jim/carmen.git"]

After a lot of poking around, it turns out you need to make two changes in order for this to work on Rails 2.3 or higher: change the git:// at the beginning of each URL to http://, and add a trailing slash to the end of each URL. So instead, run these:

script/plugin install http://github.com/binarylogic/authlogic.git/
script/plugin install http://github.com/jim/carmen.git/

They both worked perfectly for me, so hopefully they’ll work for you. If not, leave a comment and I’ll try to help.

Continue Reading

04 November 2009 ~ 0 Comments

Little Joys of Rails (Part 1) – Renaming Associations

I’ve spent the last month or so teaching myself the basics of Ruby on Rails. By far my favorite thing about it is that, at every turn, I find a little feature or helper that works really well and does exactly what I want without feeling messy or “magical”.

Unfortunately, I have no one to share these little joys with, as pretty much none of my programmer friends know Ruby or Ruby on Rails. So, I figured I’d start sharing them on here. Hopefully, Rails newbies (like myself) will learn something, and Rails veterans will be able to chuckle knowingly as they fondly look back on the glory days of learning Rails.

Renaming Associations

If you have one model that belongs_to another, the association (in both directions) is named after the models. For example, if I have Users and Courses, and I want a course to belong_to its teacher (who is a User), I would start with the following:

class User < ActiveRecord::Base
  has_many :courses
end

class Course < ActiveRecord::Base
  belongs_to :user
end

This is nice and simple, but if I want to access a course’s teacher, I have to do it like this:

my_course = Course.first
puts my_course.user # Print out the course's teacher

That’s a little confusing. What if my course also has_many students (also Users)?

my_course = Course.first
puts my_course.users.first # Print out the course's first student

So, my_course.user is the teacher, and my_course.users are the students. There’s nothing to indicate that, I just have to know it. Wouldn’t it be nicer if the associations could be named for what they represent?

class Course < ActiveRecord::Base
  belongs_to :teacher, :class_name => 'User'
end

To make this work properly, the foreign key needs to be teacher_id instead of user_id (that can be overridden too, but I won’t go into that right now). Then, you get to do this:

my_course = Course.first
puts my_course.teacher # Print out the course's teacher

Much better!

Now, I recognize that this is far from a revolutionary feature. I’m pretty sure it’s possible in Django and most other MVC frameworks. I was just very happy to bump into it.

Continue Reading