Forum: Ruby on Rails What association do I want here, has_ or belongs_to?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Efcfed8f31bf296dbfcb60ecd64239cc?d=identicon&s=25 Duane (Guest)
on 2006-05-26 15:09
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!
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-05-26 15:26
(Received via mailing list)
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
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
Efcfed8f31bf296dbfcb60ecd64239cc?d=identicon&s=25 Duane (Guest)
on 2006-05-26 15:39
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
Efcfed8f31bf296dbfcb60ecd64239cc?d=identicon&s=25 Duane (Guest)
on 2006-05-26 16:15
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?
8217faf2bfdfa7daf10135d41ddd421e?d=identicon&s=25 Jeff Cohen (jeff)
on 2006-05-26 16:27
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
This topic is locked and can not be replied to.