nested attributes bug
Created by: nesrual
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.