Associations are a set of macro-like class methods for tying objects together through foreign keys. They express relationships like "Project has one Project Manager" or "Project belongs to a Portfolio". Each macro adds a number of methods to the class which are specialized according to the collection or association symbol and the options hash. It works much the same way as Ruby’s own attr* methods. Example:

  class Project < ActiveRecord::Base
    belongs_to              :portfolio
    has_one                 :project_manager
    has_many                :milestones
    has_and_belongs_to_many :categories
  end

The project class now has the following methods (and more) to ease the traversal and manipulation of its relationships:

  • Project#portfolio, Project#portfolio=(portfolio), Project#portfolio.nil?
  • Project#project_manager, Project#project_manager=(project_manager), Project#project_manager.nil?,
  • Project#milestones.empty?, Project#milestones.size, Project#milestones, Project#milestones<<(milestone), Project#milestones.delete(milestone), Project#milestones.find(milestone_id), Project#milestones.find(:all, options), Project#milestones.build, Project#milestones.create
  • Project#categories.empty?, Project#categories.size, Project#categories, Project#categories<<(category1), Project#categories.delete(category1)

Example

files/examples/associations.png

Is it belongs_to or has_one?

Both express a 1-1 relationship, the difference is mostly where to place the foreign key, which goes on the table for the class saying belongs_to. Example:

  class User < ActiveRecord::Base
    # I reference an account.
    belongs_to :account
  end

  class Account < ActiveRecord::Base
    # One user references me.
    has_one :user
  end

The tables for these classes could look something like:

  CREATE TABLE users (
    id int(11) NOT NULL auto_increment,
    account_id int(11) default NULL,
    name varchar default NULL,
    PRIMARY KEY  (id)
  )

  CREATE TABLE accounts (
    id int(11) NOT NULL auto_increment,
    name varchar default NULL,
    PRIMARY KEY  (id)
  )

Unsaved objects and associations

You can manipulate objects and associations before they are saved to the database, but there is some special behaviour you should be aware of, mostly involving the saving of associated objects.

One-to-one associations

  • Assigning an object to a has_one association automatically saves that object and the object being replaced (if there is one), in order to update their primary keys - except if the parent object is unsaved (new_record? == true).
  • If either of these saves fail (due to one of the objects being invalid) the assignment statement returns false and the assignment is cancelled.
  • If you wish to assign an object to a has_one association without saving it, use the #association.build method (documented below).
  • Assigning an object to a belongs_to association does not save the object, since the foreign key field belongs on the parent. It does not save the parent either.

Collections

  • Adding an object to a collection (has_many or has_and_belongs_to_many) automatically saves that object, except if the parent object (the owner of the collection) is not yet stored in the database.
  • If saving any of the objects being added to a collection (via #push or similar) fails, then #push returns false.
  • You can add an object to a collection without automatically saving it by using the #collection.build method (documented below).
  • All unsaved (new_record? == true) members of the collection are automatically saved when the parent is saved.

Association callbacks

Similiar to the normal callbacks that hook into the lifecycle of an Active Record object, you can also define callbacks that get trigged when you add an object to or removing an object from a association collection. Example:

  class Project
    has_and_belongs_to_many :developers, :after_add => :evaluate_velocity

    def evaluate_velocity(developer)
      ...
    end
  end

It’s possible to stack callbacks by passing them as an array. Example:

  class Project
    has_and_belongs_to_many :developers, :after_add => [:evaluate_velocity, Proc.new { |p, d| p.shipping_date = Time.now}]
  end

Possible callbacks are: before_add, after_add, before_remove and after_remove.

Should any of the before_add callbacks throw an exception, the object does not get added to the collection. Same with the before_remove callbacks, if an exception is thrown the object doesn’t get removed.

Association extensions

The proxy objects that controls the access to associations can be extended through anonymous modules. This is especially beneficial for adding new finders, creators, and other factory-type methods that are only used as part of this association. Example:

  class Account < ActiveRecord::Base
    has_many :people do
      def find_or_create_by_name(name)
        first_name, last_name = name.split(" ", 2)
        find_or_create_by_first_name_and_last_name(first_name, last_name)
      end
    end
  end

  person = Account.find(:first).people.find_or_create_by_name("David Heinemeier Hansson")
  person.first_name # => "David"
  person.last_name  # => "Heinemeier Hansson"

If you need to share the same extensions between many associations, you can use a named extension module. Example:

  module FindOrCreateByNameExtension
    def find_or_create_by_name(name)
      first_name, last_name = name.split(" ", 2)
      find_or_create_by_first_name_and_last_name(first_name, last_name)
    end
  end

  class Account < ActiveRecord::Base
    has_many :people, :extend => FindOrCreateByNameExtension
  end

  class Company < ActiveRecord::Base
    has_many :people, :extend => FindOrCreateByNameExtension
  end

If you need to use multiple named extension modules, you can specify an array of modules with the :extend option. In the case of name conflicts between methods in the modules, methods in modules later in the array supercede those earlier in the array. Example:

  class Account < ActiveRecord::Base
    has_many :people, :extend => [FindOrCreateByNameExtension, FindRecentExtension]
  end

Some extensions can only be made to work with knowledge of the association proxy’s internals. Extensions can access relevant state using accessors on the association proxy:

  • proxy_owner - Returns the object the association is part of.
  • proxy_reflection - Returns the reflection object that describes the association.
  • proxy_target - Returns the associated object for belongs_to and has_one, or the collection of associated objects for has_many and has_and_belongs_to_many.

Association Join Models

Has Many associations can be configured with the :through option to use an explicit join model to retrieve the data. This operates similarly to a has_and_belongs_to_many association. The advantage is that you’re able to add validations, callbacks, and extra attributes on the join model. Consider the following schema:

  class Author < ActiveRecord::Base
    has_many :authorships
    has_many :books, :through => :authorships
  end

  class Authorship < ActiveRecord::Base
    belongs_to :author
    belongs_to :book
  end

  @author = Author.find :first
  @author.authorships.collect { |a| a.book } # selects all books that the author's authorships belong to.
  @author.books                              # selects all books by using the Authorship join model

You can also go through a has_many association on the join model:

  class Firm < ActiveRecord::Base
    has_many   :clients
    has_many   :invoices, :through => :clients
  end

  class Client < ActiveRecord::Base
    belongs_to :firm
    has_many   :invoices
  end

  class Invoice < ActiveRecord::Base
    belongs_to :client
  end

  @firm = Firm.find :first
  @firm.clients.collect { |c| c.invoices }.flatten # select all invoices for all clients of the firm
  @firm.invoices                                   # selects all invoices by going through the Client join model.

Polymorphic Associations

Polymorphic associations on models are not restricted on what types of models they can be associated with. Rather, they specify an interface that a has_many association must adhere to.

  class Asset < ActiveRecord::Base
    belongs_to :attachable, :polymorphic => true
  end

  class Post < ActiveRecord::Base
    has_many :assets, :as => :attachable         # The <tt>:as</tt> option specifies the polymorphic interface to use.
  end

  @asset.attachable = @post

This works by using a type column in addition to a foreign key to specify the associated record. In the Asset example, you’d need an attachable_id integer column and an attachable_type string column.

Using polymorphic associations in combination with single table inheritance (STI) is a little tricky. In order for the associations to work as expected, ensure that you store the base model for the STI models in the type column of the polymorphic association. To continue with the asset example above, suppose there are guest posts and member posts that use the posts table for STI. So there will be an additional ‘type’ column in the posts table.

  class Asset < ActiveRecord::Base
    belongs_to :attachable, :polymorphic => true

    def attachable_type=(sType)
       super(sType.to_s.classify.constantize.base_class.to_s)
    end
  end

  class Post < ActiveRecord::Base
    # because we store "Post" in attachable_type now :dependent => :destroy will work
    has_many :assets, :as => :attachable, :dependent => :destroy
  end

  class GuestPost < ActiveRecord::Base
  end

  class MemberPost < ActiveRecord::Base
  end

Caching

All of the methods are built on a simple caching principle that will keep the result of the last query around unless specifically instructed not to. The cache is even shared across methods to make it even cheaper to use the macro-added methods without worrying too much about performance at the first go. Example:

  project.milestones             # fetches milestones from the database
  project.milestones.size        # uses the milestone cache
  project.milestones.empty?      # uses the milestone cache
  project.milestones(true).size  # fetches milestones from the database
  project.milestones             # uses the milestone cache

Eager loading of associations

Eager loading is a way to find objects of a certain class and a number of named associations along with it in a single SQL call. This is one of the easiest ways of to prevent the dreaded 1+N problem in which fetching 100 posts that each needs to display their author triggers 101 database queries. Through the use of eager loading, the 101 queries can be reduced to 1. Example:

  class Post < ActiveRecord::Base
    belongs_to :author
    has_many   :comments
  end

Consider the following loop using the class above:

  for post in Post.find(:all)
    puts "Post:            " + post.title
    puts "Written by:      " + post.author.name
    puts "Last comment on: " + post.comments.first.created_on
  end

To iterate over these one hundred posts, we’ll generate 201 database queries. Let’s first just optimize it for retrieving the author:

  for post in Post.find(:all, :include => :author)

This references the name of the belongs_to association that also used the :author symbol, so the find will now weave in a join something like this: LEFT OUTER JOIN authors ON authors.id = posts.author_id. Doing so will cut down the number of queries from 201 to 101.

We can improve upon the situation further by referencing both associations in the finder with:

  for post in Post.find(:all, :include => [ :author, :comments ])

That’ll add another join along the lines of: LEFT OUTER JOIN comments ON comments.post_id = posts.id. And we’ll be down to 1 query.

To include a deep hierarchy of associations, using a hash:

  for post in Post.find(:all, :include => [ :author, { :comments => { :author => :gravatar } } ])

That’ll grab not only all the comments but all their authors and gravatar pictures. You can mix and match symbols, arrays and hashes in any combination to describe the associations you want to load.

All of this power shouldn’t fool you into thinking that you can pull out huge amounts of data with no performance penalty just because you’ve reduced the number of queries. The database still needs to send all the data to Active Record and it still needs to be processed. So it’s no catch-all for performance problems, but it’s a great way to cut down on the number of queries in a situation as the one described above.

Since the eager loading pulls from multiple tables, you’ll have to disambiguate any column references in both conditions and orders. So :order => "posts.id DESC" will work while :order => "id DESC" will not. Because eager loading generates the SELECT statement too, the :select option is ignored.

You can use eager loading on multiple associations from the same table, but you cannot use those associations in orders and conditions as there is currently not any way to disambiguate them. Eager loading will not pull additional attributes on join tables, so "rich associations" with has_and_belongs_to_many are not a good fit for eager loading.

When eager loaded, conditions are interpolated in the context of the model class, not the model instance. Conditions are lazily interpolated before the actual model exists.

Table Aliasing

ActiveRecord uses table aliasing in the case that a table is referenced multiple times in a join. If a table is referenced only once, the standard table name is used. The second time, the table is aliased as #{reflection_name}_#{parent_table_name}. Indexes are appended for any more successive uses of the table name.

  Post.find :all, :include => :comments
  # => SELECT ... FROM posts LEFT OUTER JOIN comments ON ...
  Post.find :all, :include => :special_comments # STI
  # => SELECT ... FROM posts LEFT OUTER JOIN comments ON ... AND comments.type = 'SpecialComment'
  Post.find :all, :include => [:comments, :special_comments] # special_comments is the reflection name, posts is the parent table name
  # => SELECT ... FROM posts LEFT OUTER JOIN comments ON ... LEFT OUTER JOIN comments special_comments_posts

Acts as tree example:

  TreeMixin.find :all, :include => :children
  # => SELECT ... FROM mixins LEFT OUTER JOIN mixins childrens_mixins ...
  TreeMixin.find :all, :include => {:children => :parent} # using cascading eager includes
  # => SELECT ... FROM mixins LEFT OUTER JOIN mixins childrens_mixins ...
                              LEFT OUTER JOIN parents_mixins ...
  TreeMixin.find :all, :include => {:children => {:parent => :children}}
  # => SELECT ... FROM mixins LEFT OUTER JOIN mixins childrens_mixins ...
                              LEFT OUTER JOIN parents_mixins ...

LEFT OUTER JOIN mixins childrens_mixins_2

Has and Belongs to Many join tables use the same idea, but add a _join suffix:

  Post.find :all, :include => :categories
  # => SELECT ... FROM posts LEFT OUTER JOIN categories_posts ... LEFT OUTER JOIN categories ...
  Post.find :all, :include => {:categories => :posts}
  # => SELECT ... FROM posts LEFT OUTER JOIN categories_posts ... LEFT OUTER JOIN categories ...
                             LEFT OUTER JOIN categories_posts posts_categories_join LEFT OUTER JOIN posts posts_categories
  Post.find :all, :include => {:categories => {:posts => :categories}}
  # => SELECT ... FROM posts LEFT OUTER JOIN categories_posts ... LEFT OUTER JOIN categories ...
                             LEFT OUTER JOIN categories_posts posts_categories_join LEFT OUTER JOIN posts posts_categories
                             LEFT OUTER JOIN categories_posts categories_posts_join LEFT OUTER JOIN categories categories_posts

If you wish to specify your own custom joins using a :joins option, those table names will take precedence over the eager associations..

  Post.find :all, :include => :comments, :joins => "inner join comments ..."
  # => SELECT ... FROM posts LEFT OUTER JOIN comments_posts ON ... INNER JOIN comments ...
  Post.find :all, :include => [:comments, :special_comments], :joins => "inner join comments ..."
  # => SELECT ... FROM posts LEFT OUTER JOIN comments comments_posts ON ...
                             LEFT OUTER JOIN comments special_comments_posts ...
                             INNER JOIN comments ...

Table aliases are automatically truncated according to the maximum length of table identifiers according to the specific database.

Modules

By default, associations will look for objects within the current module scope. Consider:

  module MyApplication
    module Business
      class Firm < ActiveRecord::Base
         has_many :clients
       end

      class Company < ActiveRecord::Base; end
    end
  end

When Firm#clients is called, it’ll in turn call MyApplication::Business::Company.find(firm.id). If you want to associate with a class in another module scope this can be done by specifying the complete class name, such as:

  module MyApplication
    module Business
      class Firm < ActiveRecord::Base; end
    end

    module Billing
      class Account < ActiveRecord::Base
        belongs_to :firm, :class_name => "MyApplication::Business::Firm"
      end
    end
  end

Type safety with ActiveRecord::AssociationTypeMismatch

If you attempt to assign an object to an association that doesn’t match the inferred or specified :class_name, you’ll get a ActiveRecord::AssociationTypeMismatch.

Options

All of the association macros can be specialized through options which makes more complex cases than the simple and guessable ones possible.

Show files where this module is defined (2 files)
Register or log in to add new notes.
April 23, 2009
1 thank

Using strings as association names

Beware, that using strings as association names, when giving Hash to :include will render errors:

The error occurred while evaluating nil.name

So, :include => [‘assoc1’, ‘assoc2’ ] will work, and :include => [ {‘assoc1’ => ‘assoc3’}, ‘assoc2’] won’t. Use symbols:

Proper form

:include => [ {:assoc1 => :assoc3}, ‘assoc2’]

August 8, 2008
0 thanks

finder_sql

If you are using the finder_sql option, it is important to use single quotes if need to interpolate variables, such as the id of the record. Otherwise you will get the object_id of the class.

July 26, 2010
0 thanks

Using strings as association names - beware of HashWithIndifferentAccess

If you merge a normal Hash into a HashWithIndifferentAccess, then the keys will convert to strings…

This will likely bite you if the merge is passed to AR find: as netmaniac said “Beware, that using strings as association names, when giving Hash to :include will render errors”.

Beware that params from your controller are HashWithIndifferentAccess like.

August 3, 2012
0 thanks

a misprint?

In section ‘Bi-directional associations’ an example:

d = Dungeon.first

t = d.traps.first

d.level == t.dungeon.level # => true

d.level = 10

d.level == t.dungeon.level # => false

Then use has_many associations, but lower than written ‘for belongs_to associations has_many inverse associations are ignored.’