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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:39:13+00:00 2026-05-23T16:39:13+00:00

In rails 3.0.9/ruby 1.9.2 I’m getting unexpected behaviour when i try to update model

  • 0

In rails 3.0.9/ruby 1.9.2 I’m getting unexpected behaviour when i try to update model foreign keys.

I start with a fresh install

$ rails new mytest
$ rails g model User 
$ rails g model Ad user_id:integer
$ rake db_migrate

Add association in app/models/Ad.rb

Class User < ActiveRecord::Base
  belongs_to :user
end

Now to the fun part. I want to change the foreign key on an object directly. I enter rails console with rails c

$ u = User.create
$ a=Ad.create(:user=>u)
$ a.update_attributes(:user_id => 9999)
$ a.user_id
=> 4

So that doesn’t work. I try to set the object directly instead of passing it to create:

$ u = User.create
$ a=Ad.create
$ a.user = u
$ a.save
$ a.update_attributes(:user_id => 9999)
$ a.user_id
=> 5

Doesn’t work.

The only thing that works is this:

$ u = User.create
$ a=Ad.create
$ a.user_id = u.id
$ a.save
$ a.update_attributes(:user_id => 9999)
$ a.user_id
=> 9999

Can someone please explain what is going on, and how can I change the foreign keys of my objects? I assume the “blocking” behaviour happens when the objects associations are instantiated and there is a conflict, so one answer is to do this:

$ Ad.find( a.id ).update_attributes( :user_id => xxxx )

That works. But it seems like a long way to go just for changing a foreign key on an object. It also takes an extra DB hit, and lastly it is quite messy code if an object internally needs to update itself. What to do?

  • 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-23T16:39:13+00:00Added an answer on May 23, 2026 at 4:39 pm

    Just unload the loaded object before doing the update

    a = Ad.create( :user=>User.create )
    a.user.reset   # Unload association object, the foreign key is untouched
    a.update_attributes( :user_id=>9999 )
    a.user_id
      => 9999
    

    You can make Factory girl create objects with unloaded associations like this

    def Factory.lazy( model )
        obj = Factory( model )
        obj.attributes.each{ |k,v|
        if assoc = k[/(.*)_id$/,1]
            eager = obj.send( assoc )
            eager.send("reset") unless eager.nil?
        end
        obj
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Ruby on Rails, I'm attempting to update the innerHTML of a div tag
In Ruby on Rails, how to add foreign key constraint in migration?
I am new to Rails / Ruby. I am working on a project where
I have a Rails Model: ruby-1.9.2-p0 > NavItem => NavItem(id: integer, item_identifier: string, description:
i I am new to rails and ruby. I am trying to make a
Has anyone used Ruby/Rails with a Sales Logix database?
Within Ruby on Rails applications database.yml is a plain text file that stores database
In Ruby on Rails Development (or MVC in general), what quick rule should I
my ruby (on rails) class looks like: class Foo def self.method1 someAction end def
The Ruby On Rails Wiki lists a couple of librarie s that facilitate PDF

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.