active-record episodes
#2
Mar 07
10 comments
Dynamic find_by Methods
Shorten simple finds considerably and improve readability by using the dynamic find_all_by and find_by methods.
Find Through Association
No need to pass foreign keys in find conditions, just do the find through a has_many association.
Move Find into Model
Move a find into the model to clean up the controllers and remove duplication. Also see how you can call these custom find methods through an association.
Using with_scope
Learn how to use with_scope - a very powerful method which will allow your custom find methods to accept any find options. Just like magic!
Performing Calculations on Models
Did you know ActiveRecord provides class methods for performing calculations on models? You can even use these methods through associations.
Fun with Find Conditions
You can pass more than simple strings to find conditions. Arrays, ranges, and nil values can be passed as well. In this episode you will see the tricks involved with passing these odd objects to find conditions. (Update: audio fixed).
Virtual Attributes
Keep your controllers clean and forms flexible by adding virtual attributes to your model. This very powerful technique allows you to create form fields which may not directly relate to the database.
Eager Loading
One way to improve performance is to cut down on the number of SQL queries. You can do this through eager loading. Learn all about it in this episode!
Counter Cache Column
If you need to display the record count for a has_many association, you can improve performance by caching that number in a column.
SQL Injection
One of the most common security problems for dynamic sites is SQL Injection. Thankfully Rails does everything it can in solving this issue, but you still need to be aware of it.
Hackers Love Mass Assignment
Your site may be at risk! When using mass assignment, you are giving the user complete control over that model and its associations. See how a hacker might use this vulnerability and learn how to stop it in this episode.
Time in Text Field
Although Rails does allow you to edit time attributes with text fields, it's not very flexible. In this episode you will learn how to use a virtual attribute to format the time to your liking.
Making a Plugin
You can sometimes remove a lot of duplication by generating methods dynamic. In this episode I will show you how to create a plugin which does exactly that.
Conditional Validations
By default, validations will take place every time the model is saved. Sometimes you only want a validation to happen when certain conditions are met. See how to do that in this episode.
Two Many-to-Many
There are two different ways to set up a many-to-many association in Rails. In this episode you will see how to implement both ways along with some tips on choosing the right one for your project.
Create Model Through Text Field
Let's say you provide a select menu for setting which category a given product belongs to, but you also want the option of creating a new category by typing the name in a text field. See a great way to do that in this episode.
Optimistic Locking
When two people attempt to update the same record near the same time, one of the updates will likely be overwritten. You can solve this problem with optimistic locking.
Hacking ActiveRecord
Have you ever wanted to temporarily disable all validations? Well, ActiveRecord doesn't support this, but that doesn't mean we can't add it. This episode will show you how to open up an existing class and change its behavior.
Model Name in URL
By default, Rails uses the model's id in the URL. What if you want to use the name of the model instead? You can change this behavior by overriding the to_param method in the model. Watch this episode for details.
Refactoring Long Methods
In this episode I walk you through an exercise in refactoring by taking a long method and making it more concise.
Site Wide Announcements
Sometimes you need to display an administrative announcement to every page on the site and give the users the ability to hide the announcement. See how in this episode.
named_scope
The named_scope method in Rails 2.1 makes performing finds on models very elegant and convenient. See how in this episode.
Tracking Attribute Changes
Rails 2.1 keeps track of the changes you make to a model's attributes. It also allows you to see what the previous value was. But watch out for the gotcha! See this episode for details.



