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
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
A lot of people try Test Driven Development by writing the test, writing the code, and then wondering what the big deal is. Here’s the process I follow along with an explanation of why each step helps.
As a quick note, all tests are written with the RSpec framework.
Read More
Developers, development, rspec, test driven development, testing, tests
I’ve had a few discussions with people under various circumstances regarding Test First or Test Driven Development (TDD). Some people swear by TDD, while other don’t see how it could possibly work because “I don’t know how my code works until I write it.” My answer to this is “That’s why you need to write the test first.”
My initial exposure to Test Driven Development was while working for a client at SolutionStream. The client insisted that all development be done Test First. At first, it was a painful process. The tests took a while to write, and I really just wanted to get into solving the problem. As time went on, however, I found that writing the tests first, did a few things that made coding much more pleasant.
Read More
BDD, debugging, Developers, development, TDD, Test First, testers, testing, tests
Setting up a Debug Log
Have you ever been debugging your Rails application and watching the development log fly by wishing that you could put output just the information you need to its own log? There’s actually a quick and easy way to do it.
First, you create your logger.
1
| @debug_log = Logger.new(File.open(File.dirname(__FILE__) + "/../../log/debug.log", "w")) |
Then you log data to it.
2
| @debug_log.debug @object |
It’s simple and it makes the data extremely easy to find. Just take the statements out when you’re done debugging.
debugging, development, logger, ruby errors, Ruby on Rails