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 7718899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:16:51+00:00 2026-06-01T03:16:51+00:00

I am trying to create a contact which is a has_one relation to a

  • 0

I am trying to create a contact which is a has_one relation to a client. I am doing this with nested attributes. I am properly building the contact within the “new” view/controller. When I go to save the contact it is telling me the contact must be present. So for some reason it is not creating the contact.

ERROR:

Validation failed - Contact can't be blank.

Params:

{
  "utf8"=>"✓",
  "authenticity_token"=>"ep6es356WY5dja7D7C5Kj8Qc0Yvuh3IN2Z1iGG08J7c=",
  "client"=>{
    "contact_attributes"=>{
      "first_name"=>"Scott",
      "last_name"=>"Baute"
    },
  "commit"=>"Create Client"
}

Models:

class Client
  include Mongoid::Document
  include Mongoid::Timestamps

  attr_accessible :role, :contact_id, :contact_attributes

  # Relationships
  belongs_to :firm, validate: true
  has_one :contact, validate: true, autosave: true

  # Matters is custom relationship
  has_many :client_roles

  # Nested Attr
  accepts_nested_attributes_for :contact

  validates :contact_id, presence: true

  # Fields
  field :contact_id
  field :test
end

class Contact
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::MultiParameterAttributes

  #Relationships
  belongs_to :client

  field :first_name
  field :last_name

end

Controller:

# GET /clients/new
# GET /clients/new.json
def new
  @client = current_firm.clients.new

  respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @client }
  end
end

# POST /clients
# POST /clients.json
def create
  @client = current_firm.clients.new(params[:client])

  respond_to do |format|
    if @client.save!
      format.html { redirect_to client_path(@client.contact.id), notice: 'Client was successfully created.' }
      format.json { render json: @client, status: :created, location: @client }
    else
      format.html { render action: "new" }
      format.json { render json: @client.errors, status: :unprocessable_entity }
    end
  end
end

View:

- @client.build_contact unless @client_contact
= semantic_form_for @client, html: { class: "form-horizontal"} do |f|
  .control-group
    = render "contact_fields", f: builder    
  .form-actions
    = f.submit class: "btn btn-primary"
  • 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-06-01T03:16:52+00:00Added an answer on June 1, 2026 at 3:16 am

    I think you need delete your reference to contact_id in your Client model. The has_one association do the foreign_key in your contact. Not in your client. So when you create a contact from client, only a client_id is define in your contact no contact_id in your client.

    So delete line :

    validates :contact_id, presence: true
    
    # Fields
    field :contact_id
    

    in your Client model.

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

Sidebar

Related Questions

I am trying to create a new contact using Dynamic Entity. The sample i
I'm trying to create a new contact and add it to the AddressBook but
I am new to ASP.NET, and I am trying to create a contact form
I am trying to create application, which sould work this way: User dial a
I'm new to Xcode and I'm trying to create an app which has a
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
i'm trying to create an application which connects to internet and consume web services
I`m trying to create a message which uses some kind of pluralization. The message
I am trying to create an application in Android 2.2 which sends SMS to
I am trying to create a way a user can contact a friend via

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.