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!
on 2006-05-26 17:09
on 2006-05-26 17:26
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
on 2006-05-26 17: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
on 2006-05-26 18: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?
on 2006-05-26 18: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