Showing posts with label ruby on rails. Show all posts
Showing posts with label ruby on rails. Show all posts

Saturday, January 23, 2010

Prototype analog to jQuery's $(document).ready

I have a lot of experience with jQuery, but less with Prototype. Recently, I needed to add some event handlers to some elements in a Ruby on Rails app I'm building. I searched for how to do the equivalent of jQuery's $(document).ready() function in Prototype so that I could add the handlers after the document loaded, but most of the guides I found were out of date (I'm running Prototype 1.6.0.3, and I don't know which version these guides were for, but they all made my Javascript console angry).

Eventually, I was able to piece it together after digging through the depths of the Prototype API documentation. It's actually very simple:


document.observe('dom:loaded', function(){
// do yo thang...
});


Wrap whatever you're doing with that, and it won't be run until the document is loaded.

Wednesday, November 4, 2009

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:

[code lang="rails"]
class User < ActiveRecord::Base
has_many :courses
end

class Course < ActiveRecord::Base
belongs_to :user
end
[/code]

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

[code lang="rails"]
my_course = Course.first
puts my_course.user # Print out the course's teacher
[/code]

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

[code lang="rails"]
my_course = Course.first
puts my_course.users.first # Print out the course's first student
[/code]

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?

[code lang="rails"]
class Course < ActiveRecord::Base
belongs_to :teacher, :class_name => 'User'
end
[/code]

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:

[code lang="rails"]
my_course = Course.first
puts my_course.teacher # Print out the course's teacher
[/code]

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.