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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:28:55+00:00 2026-05-20T14:28:55+00:00

I am trying to do a basic belongs_to/has_many association but running into a problem.

  • 0

I am trying to do a basic belongs_to/has_many association but running into a problem. It seems that the declaring class’s foreign key column is not being updated. Here are my models:


#
# Table name: clients
#
#  id         :integer         not null, primary key
#  name       :string(255)
#  created_at :datetime
#  updated_at :datetime
#

class Client < ActiveRecord::Base
  has_many :units
  ...
  attr_accessible :name
end

#
# Table name: units
#
#  id         :integer         not null, primary key
#  client_id  :integer
#  name       :string(255)
#  created_at :datetime
#  updated_at :datetime
#

class Unit < ActiveRecord::Base
  belongs_to :client
  ...
  attr_accessible :name
end

When I open rails console I do the following:

#This works as it should
c1 = Client.create(:name => 'Urban Coding')
u1 = c1.units.create(:name => 'Birmingham Branch')

The above gives me the correct results. I have a client and a unit. The unit has the client_id foreign key field populated correctly.

#This does not work.
c1 = Client.create(:name => 'Urban Coding')
u1 = Unit.create(:name => 'Birmingham Branch')

u1.client = c1

I feel the above should have the same effect. However this is not the case. I have a unit and a client but the units client_id column is not populated. Not sure exactly what I am doing wrong here. Help is appreciated. Let me know if you need more information.

  • 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-20T14:28:56+00:00Added an answer on May 20, 2026 at 2:28 pm

    You’re simply not saving u1, hence no change to the database.

    If you want it assigned and saved in a single operation, use update_attribute

    u1.update_attribute(:client, c1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am digging into LINQ--trying to understand basic models (it seems pretty cool to
Im trying to get into some basic JavaFX game development and I'm getting confused
I'm trying to create a basic system that will allow only specific users to
I'm trying to do some basic paging in MSSQL. The problem I'm having is
This is probably pretty basic, but I'm trying to figure out how to show
I'm trying to write a named scope that will order my 'Products' class based
I am trying to get some basic unit tests up and running on the
I'm trying a very basic XPath on this xml (same as below), and it
I'm trying to do a basic OR on three fields using a hibernate criteria
I am trying to learn some of the basic and advanced features of visual

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.