---
title: "belongs_to vs HasMany"
author: "sp42"
date: "2007-10-24T22:33:35Z"
tags: []
slug: "belongs-to-vs-hasmany"
draft: false
lastmod: "2007-10-24T22:33:35Z"
id: 135291
url: "https://sp42.iteye.com/blog/135291"
---

# Ruby on Rails belongs\_to

[belongs\_to](http://wiki.rubyonrails.org/rails/pages/belongs_to) complements a [has\_many](http://wiki.rubyonrails.org/rails/pages/has_many) or [has\_one](http://wiki.rubyonrails.org/rails/pages/has_one) association.

In general, the Foo model belongs\_to :bar if the foo table has a bar\_id foreign key column.

Examples of this:

- Companies database, where an **account** belongs to a **company**.
- Forum system, where a **topic** belongs to a **forum**, which belongs to a **category**.
- a gallery system where a **thumbnail** belongs to a **picture**.

## Example Forum

See [ForumExample](http://wiki.rubyonrails.org/rails/pages/ForumExample).

## Notes

When using `:counter_cache => true`, be sure the default value on your model\_count column defaults to something that can be incremented. NULL + 1, for instance, equals NULL so the counter SQL won’t work.

You’d think this feature would make parent.child.count or parent.child.length use this cache, instead of issuing a new COUNT query. You’d be wrong.

See the [API](http://wiki.rubyonrails.org/rails/pages/BROKENLINK) for more information about `:counter_cache`

Another way to think of this is to say “the foreign key in table abc [belongs\_to](http://wiki.rubyonrails.org/rails/pages/BROKENLINK) table def”.

## Gotchas

Watch your plural versus singular! If you’re going between has\_many and belongs\_to, it’s easy to get mixed up with plurals and singulars. belongs\_to definitely uses singular parameters, and will not print very convenient errors if not put correctly.

If you are using belongs\_to across namespaces, you will definitely need to use the :class\_name and :foreign\_key modifiers to belongs\_to.

Say you are saying your Model belongs\_to MySpace::OtherModel

```

class Model
  belongs_to :other_model # will NOT work
  # The following WILL work
  belongs_to :other_model,
             :class_name => "MySpace::OtherModel",
             :foreign_key => "other_model_id" 
end

```

Updated on September 24, 2007 10:29 by [Anonymous Coward](http://wiki.rubyonrails.org/rails/pages/Anonymous+Coward) (131.246.195.18)

# Ruby on Rails has\_many

The [has\_many](http://wiki.rubyonrails.org/rails/pages/has_many) association provides a way to access a one way relation between two tables.

Examples of this:

- Companies database, where a company may have many accounts.
- Forum System, where a category has many forums, which has many topics.

## Example Forum

See [ForumExample](http://wiki.rubyonrails.org/rails/pages/ForumExample).

## See Also

- [belongs\_to](http://wiki.rubyonrails.org/rails/pages/belongs_to)
- [has\_one](http://wiki.rubyonrails.org/rails/pages/has_one)
- [Rails API: has\_many](http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000530)
- [Rails Manual : has\_many](http://www.railsmanual.org/module/ActiveRecord%3A%3AAssociations%3A%3AClassMethods/has_many)

## Notes

The **finder\_sql** option in has\_many should be in single quotes if you want inline variables (e.g. ‘SELECT * FROM something WHERE user\_id = #{id}’) to be evaluated properly!