rails: Possible circular references bug

UPDATE: Code to reproduce the problem is in comments

I have a Rails project with the following models:

account
username
user
address

With the following associations:

class Account < ActiveRecord::Base
  has_many :users, :inverse_of => :account
  has_many :addresses, :inverse_of => :account
  has_many :usernames, :inverse_of => :account

  accepts_nested_attributes_for :usernames
  accepts_nested_attributes_for :users
  accepts_nested_attributes_for :addresses
end

class Username < ActiveRecord::Base
  belongs_to :account
end

class User < ActiveRecord::Base
  belongs_to :account
  belongs_to :username
end

class Address < ActiveRecord::Base
  belongs_to :account
end

I build a new account:

account = Account.new(params.require(:account).permit(
    usernames_attributes: [:username], 
    users_attributes: [:password, :firstname, :lastname, :phone, :mobile, :fax],
    addresses_attributes: [:address_line_1, :address_line_2, :city, :zipcode, :state, :country]))

which validates and saves fine under Rails 4.2.0, however with Rails 4.2.1 I get the following error returned from PostgreSQL:

 ActiveRecord::StatementInvalid:
   PG::NotNullViolation: ERROR:  null value in column "account_id" violates not-null constraint

The error is on the username model when trying to save it. Seems like the reference to the account is missing when saving.

About this issue

  • Original URL
  • State: open
  • Created 12 years ago
  • Comments: 15 (7 by maintainers)

Most upvoted comments

This is still reproducible on Rails 7.0.0-alpha

# frozen_string_literal: true

begin
  require "bundler/inline"
rescue LoadError => e
  $stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
  raise e
end

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem "rails", github: "rails/rails", branch: "main"
  gem "sqlite3"
end

require 'active_record'
require 'minitest/autorun'
require 'logger'

# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :accounts, force: true  do |t|
  end

  create_table :usernames, force: true  do |t|
    t.column :account_id, :integer, :index => true, :null => false
    t.column :username, :string, :default => '', :null => false
  end

  create_table :users, force: true  do |t|
    t.column :account_id, :integer, :null => false, :index => true
    t.column :username_id, :integer, :null => false
  end
end

class Account < ActiveRecord::Base
  has_many :users, :inverse_of => :account
  has_many :usernames, :inverse_of => :account
  accepts_nested_attributes_for :usernames
  accepts_nested_attributes_for :users
end

class User < ActiveRecord::Base
  belongs_to :account
  belongs_to :username
  validates_presence_of :account
  validates_presence_of :username
end

class Username < ActiveRecord::Base
  belongs_to :account
  validates_presence_of :account
end

class BugTest < Minitest::Test
  def test_association_stuff
    account = Account.new
    account.usernames.build
    account.users.build
    account.users.first.username = account.usernames.first

    assert_equal 0, Account.all.size
    assert true, account.valid?

    begin
      account.save
    rescue Exception => e
       # Rails 4.2.0 saves the record fine
    end

    assert_equal 1, Account.all.size
  end
end