I started reading ActiveRecord::Base a few days ago and found 8 things that I didn’t know about that it offered. I also only made it about 1/4 of the way through the code. Here are a few new things I’ve learned upon further reading:
1. Find by multiple ids
Not only can you find a single record by calling find_by_id, you can find multiple records by providing an array of ids.
User.find_by_id([1, 12, 55])
# Returns 3 User objects with ids of 1, 12, and 55. If any isn't found, then RecordNotFound is raised.
2. Locking database records
If you have multiple processes that may update the same record (like incrementing a counter), then you may run into a problem where they both pull the record when the counter = 42. They each update the counter to 43 and save the record. This results in a deviation from reality of 1.
The solution is to lock the record while updating it. Here’s the code: Read More
activerecord, data, Database, development, Rails, Ruby, Ruby on Rails
I’ve been trying to read more code lately and figured that I’d be best served by reading code I use frequently. Here are some notes of things I gathered from reading the ActiveRecord base.rb file.
Read More
activerecord, api rails base query attributes
I just started building a new Rails application in version 2.3.4. One feature that I thought was particularly handy is the data seeding that is now built into Ruby on Rails.
Before this feature, you would have to do one of two things. You could seed your data in your migrations. The problem with this approach is that it clutters up your migrations, and can make for more brittle migrations. It also may or may not propagate to your test database when you run your tests, meaning that if you’re counting on it, it may not bee there.
Your second option was to create fixtures and a rake task to import the fixture data into your Rails application. The problem with this is the need to create multiple related objects across multiple files to make all of your data match up, which can create maintenance problems.
So, without further ado, here is the solution now included in Rails. You simple create a file at db/seeds.rb and place ActiveRecord create calls in the file. Here’s an example seeds.rb.
Read More
2.3.4, activerecord, create, data, data seeding, Database, fixtures, migration, rake
One of the most powerful aspects of Ruby on Rails are the associations we can create between two classes. It is immensely convenient to be able to call person.posts rather than doing a SQL statement to find all of the posts with a person_id of X.
Sometimes, we have instances where the associations could be with multiple classes. For example, if we have a Page class that can be associated with an HTMLAdvertisement or an ImageAdvertisement. In that case, we really want to be able to call @page.advertisement to get the advertisement. This is where polymorphic associations come in.
Polymorphic associations allow us to associate a single attribute of the class to any number of specified classes. Here are the Page, HtmlAdvertisement, and ImageAdvertisement models:
Read More
activerecord, Database, migration, mixin modules, polymorphic associations, restful routes
A lot of new Ruby developers I’ve worked with have seen the symbol notation—starting with a :— and have been confused by what a symbol actually is. There is a lot of information out there that is confusing as well. Here’s a brief rundown of what symbols are and how they are used.
Read More
activerecord, Developers, development, Ruby, symbol