views:

994

answers:

1

I have a model called Contact which has_one guest like so.

class Contact < ActiveRecord::Base
  has_one :guest, :class_name => "Contact", :foreign_key => "guest"
end

In my create view I'm using the select helper method to list all Contacts that can be chosen as a guest like so...

<% form_for(@contact) do |f| %>
  <%= f.error_messages %>
    ...
    <p>
     <%= f.label :guest %><br />
     <% @guests = Contact.find(:all, :order => "lastname").map { |u| [u.lastname + ', ' + u.firstname, u.id] } %>
     <%= f.select :guest, @guests, {:include_blank => true} %>
    </p>
    ...
<% end %>

Whenever this form is submitted I get an error saying that it was expecting a contact but got a string.

Where and how should I convert the string submitted by the form to a contact before I save the current contact to the database?

+3  A: 

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!

BushyMark
Yep. My use case is the workaround scenario...Thanks man.
Jason Punyon