When you create an association in Rails, such as has_one, there are certain methods that a dynamically generated for your object for easy assignment.
Right now, the method you are using is
@contact.guest = @guest
And your contact object is expecting to receive another ActiveRecord object that it may associate itself with. What you are trying to do though, is kind "fake" this method in your form that wraps a model object. (The information coming in from the params is brought in as a string, which is why you are getting that error)
There are several ways you could go about implementing this. The first with is using nested model attributes . . . complex forms that allow you to wrap a child association within the form_for
you are using (using the fields_for
form helper). An explanation of this would take a long time to type up, and fortunately there are incredible free resources on complex forms:
Railscasts Complex Forms part 1
Railscasts Complex Forms part 2
Railscasts Complex Forms part 3
Also, if you are using Rails 2.3 then check out some information on "accepts_nested_attributes_for
" . . . There is a distinct lack of tutorials out there right now, but I imagine with the release of 2.3 there are going to be plenty coming. This handy little helper will cut out about half of the code Ryan uses in his screen casts to make forms accept a subbordinate model.
Finally there is a workaround. If your relationship is simple, if the guests you want to associate are already in the database, and you don't need to create new ones from the form you are using, then you can use something like a virtual attribute. In your form, simply change your f.select :guest
to something like f.select :guest_select
and implement a method in your model:
def guest_select=(incoming_id_from_form)
contact = Contact.find(incoming_id_from_form)
self.guest = contact
end
Which will assign the ActiveRecord model without having to implement a complex form to do it for you!