Skip to content

GitLab

  • Menu
Projects Groups Snippets
    • Loading...
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in
  • G gitlabhq1
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 21
    • Issues 21
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 12
    • Merge requests 12
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Monitor
    • Monitor
    • Incidents
  • Packages & Registries
    • Packages & Registries
    • Package Registry
    • Infrastructure Registry
  • Analytics
    • Analytics
    • CI/CD
    • Repository
    • Value stream
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • gpt
  • large_projects
  • gitlabhq1
  • Issues
  • #9038

Closed
Open
Created Mar 27, 2015 by Administrator@rootOwner

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.

Assignee
Assign to
Time tracking