Rails didn’t move a mountain.
Rails didn’t part the Red Sea.
Rails only really had to do one thing to make me a convert:
Rails read my mind. That’s it!
Here’s what happened. I’m building a relatively simple little webapp, but was having an issue of how I wanted to handle some functionality over the weekend. I had two models, a service item and a service. The service item had a price and the multiple service items that belong to a service made up the total for the service.
I initially started out with a one to many relationship between a service and service item but then ran into a problem: If I updated the price of a service item, I didn’t want to update the price of *every* service item that had previously been associated with a service. I briefly flirted with the idea of just duplication entries in the service items table, which would work, but soon became unweildly as I try to juggle between a set of possible service items and the set of cloned service items that were actually associated with a service.
It was a bad idea and quickly crashed.
The solution I came up with was to change the relationship to a many to many relationship through a service_service_items table. Then I would keep the current price for a service item in the service item table and then store the price for a particular service >> service_item relationship in the join table. Should be relatively simple I thought, wouldn’t take too much extra code to update the price of the join table on save and remember to only read the data from the join table. I’d have to probably create a model for the join table but some extra convenience methods in the model and I’d be gold.
So I got started.
I created my join table, updated each of my models to specify the relationship to has_and_belongs_to_many (habtm) and ran rake db:migrate
I thought I’d use the interactive console to create some services and service items to make sure the relationship worked, then I’d get down to writing the extra code to manage storing / retrieving the price in the relationship column.
Created the service, saved it and checked the database.
Good it was in the table
Created a service item, assigned it a price then added it to the service_items collection of the service.
Saved the service and checked the database.
The service item was saved through the collection. Perfect.
Checked the relation table to make sure it had a row reflecting the relationship I just created:

I looked at the database table for a second, trying to figure out what had gone wrong! Then after a couple seconds I realized that Rails just KNEW what I wanted to do. since I named the field in the relation table the same as the service_item, it automatically just added that data to the relation table for me.
This was too good to be true. I decided to update the price on the service item and save it by itself.
The price updated but the history in the relationship stayed intact!
I grabbed a copy of the service from the database and checked the price on the related service_item:
It read the old price!
Rails was handling the entire operation for me!
That sealed the deal. I officially love Rails.
Okay so this is a relatively simple thing and a common solution for a common problem.
But this is the great thing about Rails. It just does things for you.
I was excited! When you’ve been programming for many years, it feels good to be excited about a language or framework.
Whoever came up with this feature is a great man.
Like Ghandi.
Thank you web development Ghandi.