You are here: Home // ActiveRecord

026 RC Counter Caches

Podcast: Play in new window | Download A counter cache is a handy way to speed up queries where you only need the number of an associated model rather than the entire collection. In your code, you might see something like this: @post.comments.count When you run this code, you wind up with another query to the database to get that number. That’s not a big deal until you have that count being... 

025 RC Eager Loading

Podcast: Play in new window | Download Eager loading is a terrific way of speeding up your response times. Let’s consider a piece of code that pulls a list of associated objects from the database. For example, a view that displays the posts written by a given user: <% @user.posts.each do |post| %> <%= render post %> <% end %> (I know that partial has a collection option... 

ActiveRecord Observers

Podcast: Play in new window | Download About a month ago, I talked about ActiveRecord Callbacks. Observers are a way of moving callbacks out of the Model. Usually this is done to adhere to the Single Responsibility principle. So, for example, programmers will move sending an email when a record is updated or created to an Observer. class UserObserver < ActiveRecord::Observer def after_create(user) ... 

ActiveModel – Making Ruby Classes Behave Like Models

Podcast: Play in new window | Download I’ve been working on an ORM for Cassandra. One of the things I’ve been using to build the ORM is ActiveModel. ActiveModel gives you modules you can add to your class that makes it behave like an ActiveRecord Model. Here are some of the features you can add to your classes with ActiveModel. Attribute Methods Callbacks Dirty Attributes (knowing which attributes... 

Polymorphic Associations

Polymorphic Associations
Podcast: Play in new window | Download Rails has the concept of Polymorphic associations, which are associations that can be of different data types. For example, let’s say we have a Comment model. A comment in your app can be on a post or a page. Rather than creating a PostComment model and a PageComment model, you can set up your Comment model to have a polymorphic association to a Page or... 

Model Associations

Podcast: Play in new window | Download ActiveRecord models are based upon tables in relational databases. This, of course, means that they can be relational. Models can associate in three main ways: one to many, one to one, and many to many. One to many is usually achieved by calling has_many on the model representing the “one” to state that it “has many” of its counterparts. The model... 

ActiveRecord Callbacks

Podcast: Play in new window | Download You can get a full list of the ActiveRecord callbacks here. Callbacks are a handy way of insuring specific behaviors on your models as well as managing the events that follow the callbacks. For example, a before_destroy callback can be called to destroy associated objects. (This is best done on the association with :dependent => :destroy, but it makes... 

008 RC ActiveRecord Migration Gotchas

Podcast: Play in new window | Download Rails Migrations are generally pretty straightforward, but there are a few things that people do that wind up giving them headaches later on. First, don’t change migrations once they’ve been committed or deployed. This causes problems because ActiveRecord tracks migrations that have already been run. So, editing a migration that’s already... 

004 RC Ruby on Rails Model Validations

Podcast: Play in new window | Download You can get the video version of this podcast here. I’ve also updated the audio files for episodes 2 & 3 so you can hear them now. Sorry for the trouble. Ruby on Rails provides some excellent ways to validate the data you save in your models. Validations are methods called within the class definition specifying an attribute to check and what to... 

003 RC Single Table Inheritance (STI)

Podcast: Play in new window | Download Single Table Inheritance is a slightly advanced topic in Ruby on Rails. It stems from class inheritance, which is a core Object Oriented Programming principle. In Ruby, if you inherit one class from another, you use the ‘<’ operator. Here’s an example: class Truck < Car end In Rails, this gets tricky because when inheriting a Model,... 
Copyright © 2009 Rails Coach. All rights reserved.
Designed by Theme Junkie. Powered by WordPress.