# Rails associations: Easy Question requires dumb answer

**URL:** <https://www.ruby-forum.com/t/rails-associations-easy-question-requires-dumb-answer/97895>\
**Category:** Rails\
**Created:** [July 11, 2006, 5:55am UTC](https://www.ruby-forum.com/t/rails-associations-easy-question-requires-dumb-answer/97895 "2006-07-11T05:55:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [July 11, 2006, 5:55am UTC](https://www.ruby-forum.com/t/rails-associations-easy-question-requires-dumb-answer/97895/1 "2006-07-11T05:55:11Z")

</div>

_ **Scroll down to see the dumb quick question** _

I have two Models:

class Template \< ActiveRecord::Base  
end

which has this migration:  
/snip  
t.column :name, :string

And the other model:

class Product \< ActiveRecord::Base  
has\_one :template  
end

with the migration:  
/snip

```
  t.column :qty, :integer, :default => 0
  t.column :specialDescription, :string
  t.column :template_id, :integer

```

Finally here is my controller code:

/snip  
t=Product.create(:specialDescription=\>“Cleans shit”,:qty=\>12)  
t.template=Template.find(:first)  
t.save

My intention is to have several Products reference one template. But  
when I execute the above snip, I get this:

Unknown column ‘templates.product\_id’ in ‘where clause’: SELECT \* FROM  
templates WHERE (templates.product\_id = 1)

Heres the dumb question:

Why didn’t that work? Obviously rails is looking in templates for a  
productid, but I told it the exact opposite, that a product has a  
templateid. WTF MATE???

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [July 11, 2006, 6:47am UTC](https://www.ruby-forum.com/t/rails-associations-easy-question-requires-dumb-answer/97895/2 "2006-07-11T06:47:27Z")

</div>

class Template \< ActiveRecord::Base  
belongs\_to :product  
end

?

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [July 11, 2006, 6:58am UTC](https://www.ruby-forum.com/t/rails-associations-easy-question-requires-dumb-answer/97895/3 "2006-07-11T06:58:32Z")

</div>

brez! !! wrote:

> class Template \< ActiveRecord::Base  
> belongs\_to :product  
> end
> 
> ?

Same error ☹

I am so confused

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [July 11, 2006, 7:25am UTC](https://www.ruby-forum.com/t/rails-associations-easy-question-requires-dumb-answer/97895/4 "2006-07-11T07:25:10Z")

</div>

okay…

your products table has a template\_id column right?

then Product belongs\_to :template (the table with the \*\_id column is  
the one that ‘belongs\_to’ something else).

and you probably also want to say that Template has\_many :products

Make sense?

## Trevor

Trevor S.  
[http://somethinglearned.com](http://somethinglearned.com)

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [July 11, 2006, 8:02am UTC](https://www.ruby-forum.com/t/rails-associations-easy-question-requires-dumb-answer/97895/5 "2006-07-11T08:02:07Z")

</div>

That seems so backwards… but it works.

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [July 11, 2006, 7:49am UTC](https://www.ruby-forum.com/t/rails-associations-easy-question-requires-dumb-answer/97895/6 "2006-07-11T07:49:22Z")

</div>

class Template \< ActiveRecord::Base  
belongs\_to :product  
end

?

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [July 11, 2006, 8:03am UTC](https://www.ruby-forum.com/t/rails-associations-easy-question-requires-dumb-answer/97895/7 "2006-07-11T08:03:40Z")

</div>

Jon wrote:

> That seems so backwards… but it works.

Whoops posted too soon. I meant to Quote Trevor’s Response was the  
solution that worked for the benefit of anyone else with a similar  
problem

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [July 11, 2006, 8:14am UTC](https://www.ruby-forum.com/t/rails-associations-easy-question-requires-dumb-answer/97895/8 "2006-07-11T08:14:31Z")

</div>

On 7/11/06, Jon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> That seems so backwards… but it works.

In your case, product belongs\_to template. That means that template  
has\_many products.

If you have something that has\_many anythings in a db schema, that table  
can  
not include the relationship foreign\_key.

If template were to include the product\_id with which it is associated,  
that  
template can only refer to one product at a time since there is only one  
column. Conversly a product could have many templates that have it’s  
product\_id in the product\_id column of the templates table. So in your  
case you need it the other way around.

This is the way that this situation is modelled in a relational db.

In Rails, if a table includes a reference to another table, the model  
for  
that table, belongs\_to that other model. Belongs to will look within  
the  
models own table for the foreign key, where has\_many, and has\_one will  
look  
in the assocated models table.

Does that make it any clearer or have I rambled a bit much? 😉

–
