Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 207157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:42:38+00:00 2026-05-11T17:42:38+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T17:42:38+00:00Added an answer on May 11, 2026 at 5:42 pm

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 181k
  • Answers 181k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use substr: echo "<a href=\"" . $_SERVER['PHP_SELF'] .… May 12, 2026 at 4:12 pm
  • Editorial Team
    Editorial Team added an answer I have answered the similar question earlier regarding JS memory… May 12, 2026 at 4:12 pm
  • Editorial Team
    Editorial Team added an answer I can't really see that there should be a problem… May 12, 2026 at 4:12 pm

Related Questions

I know two approaches to Exception handling, lets have a look at them. Contract
In my application at the moment I have (as in so many other applications)
I have a few models that need to have custom find conditions placed on
I've just been reading Chad Fowler's blog post about 20 Rails Development No-Nos .

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.