# What association do I want here, has\_ or belongs\_to?

**URL:** <https://www.ruby-forum.com/t/what-association-do-i-want-here-has-or-belongs-to/61718>\
**Category:** Rails\
**Created:** [May 26, 2006, 3:09pm UTC](https://www.ruby-forum.com/t/what-association-do-i-want-here-has-or-belongs-to/61718 "2006-05-26T15:09:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Duane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b4bc9f/32.png) [@Duane](https://www.ruby-forum.com/u/Duane)\
**Post date:** [May 26, 2006, 3:09pm UTC](https://www.ruby-forum.com/t/what-association-do-i-want-here-has-or-belongs-to/61718/1 "2006-05-26T15:09:52Z")

</div>

I thought I had a handle on this one but it’s causing me grief and I’m  
hoping somebody can set me straight. I have an object AlertTemplate  
which will contain a fill in the blank style hunk of web content. I  
then have an object, Alert, which will have a relationship to one of  
those AlertTemplate objects, plus some data.  
So, in human speak, I’d think that an Alert has\_one AlertTemplate. But  
I can’t really say that AlertTemplate belongs\_to Alert, because each  
template may be used by many alerts. So AlertTemplate just as an id  
column, but Alert has a template\_id column.

In my form for creating an alert I have:

collection\_select(:alert, :template, (AlertTemplate.find :all), :id,  
:name)

to produce a dropdown for choosing which template you want to associate  
with the new Alert object.

However in my controller’s create method when I do an  
Alert.new(params[:alert])  
I get an error :

ActiveRecord::AssociationTypeMismatch in AlertController#create

AlertTemplate expected, got String

All the stack trace tells me is that the error happens on the Alert.new  
line. But that’s where I’m lost. Which code expected an AlertTemplate?  
What String did it get?

Interestingly I tried an experiment in irb. I created an AlertTemplate  
object t, then created an empty Alert a, and said a.template = t and  
tried to save it. The Alert object got saved – but template\_id  
remained null. That leads me to believe that I’ve got me id fields  
wrong?

Thanks!

---

<div class="post-metadata">

**Author:** ![Duane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b4bc9f/32.png) [@Duane](https://www.ruby-forum.com/u/Duane)\
**Post date:** [May 26, 2006, 3:26pm UTC](https://www.ruby-forum.com/t/what-association-do-i-want-here-has-or-belongs-to/61718/2 "2006-05-26T15:26:45Z")

</div>

On Friday, May 26, 2006, at 3:09 PM, Duane wrote:

> In my form for creating an alert I have:
> 
> tried to save it. The Alert object got saved – but template\_id  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)  
> I have a couple of tricks I use for creating ‘template’ objects. One of  
> them is to populate a ‘created\_by’ attribute with the current user id.  
> I then use the convention that when ‘created\_by’ is NULL, that item is a  
> template object.

Then you can find all the ‘template’ objects by searching for those  
‘created\_by’ NULL. If a user calls ‘new’ with a params[:id]  
correstponding to a template object, I clone that object and use it to  
populate the form.

You can use other fields to keep track of which ones are templates.  
Association ids are also good. You will need a special action to edit  
the template object as well.

This avoids having to explicity create a ‘template’ class, which I think  
is DRYer.

\_Kevin

---

<div class="post-metadata">

**Author:** ![Duane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b4bc9f/32.png) [@Duane](https://www.ruby-forum.com/u/Duane)\
**Post date:** [May 26, 2006, 3:39pm UTC](https://www.ruby-forum.com/t/what-association-do-i-want-here-has-or-belongs-to/61718/3 "2006-05-26T15:39:32Z")

</div>

Found it, I think:

> collection\_select(:alert, :template, (AlertTemplate.find :all), :id,  
> :name)

if I replace :template with ‘template\_id’, then it does the right thing.  
Which now I understand, now that I see it. The dropdown is providing  
back the id, not the object itself.

D

---

<div class="post-metadata">

**Author:** ![Duane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b4bc9f/32.png) [@Duane](https://www.ruby-forum.com/u/Duane)\
**Post date:** [May 26, 2006, 4:15pm UTC](https://www.ruby-forum.com/t/what-association-do-i-want-here-has-or-belongs-to/61718/4 "2006-05-26T16:15:21Z")

</div>

Duane wrote:

> Found it, I think:
> 
> > collection\_select(:alert, :template, (AlertTemplate.find :all), :id,  
> > :name)
> 
> if I replace :template with ‘template\_id’, then it does the right thing.  
> Which now I understand, now that I see it. The dropdown is providing  
> back the id, not the object itself.
> 
> D

Nope, that’s not working. That allows me form to work, but I can’t  
access alert.template as a nested object, which is really what I need.  
What the heck am I missing? Does the template object have to have an id  
reference back to alert? Even though it doens’t belong to only one of  
them?

---

<div class="post-metadata">

**Author:** ![Duane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b4bc9f/32.png) [@Duane](https://www.ruby-forum.com/u/Duane)\
**Post date:** [May 26, 2006, 4:27pm UTC](https://www.ruby-forum.com/t/what-association-do-i-want-here-has-or-belongs-to/61718/5 "2006-05-26T16:27:34Z")

</div>

Duane wrote:

> So AlertTemplate just as an id column, but Alert has a template\_id column.

Shouldn’t the Alert table have an “alert\_template\_id” column instead?

Jeff
