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

  • Home
  • SEARCH
  • 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 8921809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:39:17+00:00 2026-06-15T06:39:17+00:00

I have three models, Account, User and Contact: class User < ActiveRecord::Base has_one :account

  • 0

I have three models, Account, User and Contact:

class User < ActiveRecord::Base
  has_one :account

  has_many :contacts, :through => :account
end

class Account < ActiveRecord::Base
  belongs_to :owner, :class_name => 'User'

  has_many :contacts
end

class Contact < ActiveRecord::Base
  belongs_to :account
end

I’m trying to scope build a new contact through the user record, like this in my contacts controller.

def create
    @contact = current_user.contacts.build(params[:contact])

    respond_to do |format|
      if @contact.save
        ...
      else
        ...
      end
    end
end

When I do this, I don’t receive any errors, the contact record is saved to the database however the account_id column is not set on the contact, and it is not added to the collection so calling @current_user.contacts returns an empty collection.

Any suggestions?

  • 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-15T06:39:18+00:00Added an answer on June 15, 2026 at 6:39 am

    Using build makes a new instance of Contact in memory, but you would need to manually set the account_id on the record (e.g. @contact.account_id = current_user.account.id), or perhaps set it in a hidden field in the new form used to display the contact for creation such that it is picked up in the params array passed to the build method.

    You might also want to consider whether accepts_nested_attributes_for may be helpful in this case. Another option may be to use delegate, although in both cases, your use may be sort of the opposite of what these are intended for (typically defined on the “parent”).

    Update:
    In your case, the build method is added to both the User instance and to the Account (maybe “Owner”) instance, because you have both a many-to-many relationship between User and Contact, as well as a one-to-many relationship between Account and Contact. So to get the account_id I think you would need to call Account’s build, like

    @contact = current_user.accounts.contacts.build(params[:contact])
    

    Does this work?

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

Sidebar

Related Questions

I have three models: class Book < ActiveRecord::Base has_many :collections has_many :users, :through =>
I have three models: class Address < ActiveRecord::Base has_many :jobs end class Category <
I have three models: class A < ActiveRecord::Base has_many :bs end class B <
I have three models that look something like this: class Bucket < ActiveRecord::Base has_many
I have an Account model and a User model: class Account < ActiveRecord::Base has_many
I have two model as follows class User < ActiveRecord::Base validates_associated :account end class
I have three models: class User include Mongoid::Document field :name, :type => String has_many
I have three models: User Image Group . User belongs to an image; Group
I have three models: Post , Comment , User and Vote . I'm using
I've encountered some problems with MongoID. I have three models: require 'mongoid' class Configuration

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.