{"action":"created","issue":{"url":"https://api.github.com/repos/rails/rails/issues/21041","labels_url":"https://api.github.com/repos/rails/rails/issues/21041/labels{/name}","comments_url":"https://api.github.com/repos/rails/rails/issues/21041/comments","events_url":"https://api.github.com/repos/rails/rails/issues/21041/events","html_url":"https://github.com/rails/rails/issues/21041","id":97519613,"number":21041,"title":"Can't set HABTM association: Validation failed: Left side must exist","user":{"login":"masciugo","id":454321,"avatar_url":"https://avatars.githubusercontent.com/u/454321?v=3","gravatar_id":"","url":"https://api.github.com/users/masciugo","html_url":"https://github.com/masciugo","followers_url":"https://api.github.com/users/masciugo/followers","following_url":"https://api.github.com/users/masciugo/following{/other_user}","gists_url":"https://api.github.com/users/masciugo/gists{/gist_id}","starred_url":"https://api.github.com/users/masciugo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/masciugo/subscriptions","organizations_url":"https://api.github.com/users/masciugo/orgs","repos_url":"https://api.github.com/users/masciugo/repos","events_url":"https://api.github.com/users/masciugo/events{/privacy}","received_events_url":"https://api.github.com/users/masciugo/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/rails/rails/labels/activerecord","name":"activerecord","color":"0b02e1"},{"url":"https://api.github.com/repos/rails/rails/labels/needs%20feedback","name":"needs feedback","color":"ededed"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2015-07-27T18:00:20Z","updated_at":"2015-07-28T18:56:41Z","closed_at":null,"body":"I am using rails 5, cloned today from master. It seems I cannot update habtm association in veri basic examples. I have 2 models:\r\n\r\n    class User < ActiveRecord::Base\r\n      has_and_belongs_to_many :projects\r\n    end\r\n\r\n    class Project < ActiveRecord::Base\r\n      has_and_belongs_to_many :users\r\n    end\r\n\r\nI always got `ActiveRecord::RecordInvalid: Validation failed: Left side must exist` whenever I try to update the association. [Here](https://github.com/masciugo/test_habtm) is a sample application with some failing tests:\r\n\r\n**/test/models/user_test.rb**\r\n\r\n    require 'test_helper'\r\n\r\n    class UserTest < ActiveSupport::TestCase\r\n      test \"assign habtm by objects\" do\r\n        john = User.find_by(name: 'john')\r\n        john.projects = Project.all\r\n        assert_equal 2, john.projects.count\r\n      end\r\n      test \"assign habtm by ids\" do\r\n        john = User.find_by(name: 'john')\r\n        john.project_ids = Project.ids\r\n        assert_equal 2, john.projects.count\r\n      end\r\n      test \"assign habtm by inserting\" do\r\n        john = User.find_by(name: 'john')\r\n        john.projects << Project.first\r\n        assert_equal 1, john.projects.count\r\n      end\r\n      test \"assign habtm by creating\" do\r\n        john = User.find_by(name: 'john')\r\n        proj = john.projects.build(title: 'yo')\r\n        proj.save\r\n        assert_equal 1, john.projects.count\r\n      end\r\n    end\r\n\r\nand this is the output:\r\n\r\n    masciugo@macbookpro ~/dev/test_habtm $ bin/rake test\r\n    Run options: --seed 60643\r\n\r\n    # Running:\r\n\r\n    EEFE\r\n\r\n    Finished in 0.153089s, 26.1287 runs/s, 6.5322 assertions/s.\r\n\r\n      1) Error:\r\n    UserTest#test_assign_habtm_by_objects:\r\n    ActiveRecord::RecordInvalid: Validation failed: Left side must exist\r\n        test/models/user_test.rb:6:in `block in <class:UserTest>'\r\n\r\n\r\n      2) Error:\r\n    UserTest#test_assign_habtm_by_ids:\r\n    ActiveRecord::RecordInvalid: Validation failed: Left side must exist\r\n        test/models/user_test.rb:11:in `block in <class:UserTest>'\r\n\r\n\r\n      3) Failure:\r\n    UserTest#test_assign_habtm_by_creating [/Users/masciugo/dev/test_habtm/test/models/user_test.rb:23]:\r\n    Expected: 1\r\n      Actual: 0\r\n\r\n\r\n      4) Error:\r\n    UserTest#test_assign_habtm_by_inserting:\r\n    ActiveRecord::RecordInvalid: Validation failed: Left side must exist\r\n        test/models/user_test.rb:16:in `block in <class:UserTest>'\r\n\r\n    4 runs, 1 assertions, 1 failures, 3 errors, 0 skips\r\n\r\n    Failed tests:\r\n\r\n    bin/rails test test/models/user_test.rb:4\r\n    bin/rails test test/models/user_test.rb:9\r\n    bin/rails test test/models/user_test.rb:19\r\n    bin/rails test test/models/user_test.rb:14\r\n"},"comment":{"url":"https://api.github.com/repos/rails/rails/issues/comments/125718918","html_url":"https://github.com/rails/rails/issues/21041#issuecomment-125718918","issue_url":"https://api.github.com/repos/rails/rails/issues/21041","id":125718918,"user":{"login":"pixeltrix","id":6321,"avatar_url":"https://avatars.githubusercontent.com/u/6321?v=3","gravatar_id":"","url":"https://api.github.com/users/pixeltrix","html_url":"https://github.com/pixeltrix","followers_url":"https://api.github.com/users/pixeltrix/followers","following_url":"https://api.github.com/users/pixeltrix/following{/other_user}","gists_url":"https://api.github.com/users/pixeltrix/gists{/gist_id}","starred_url":"https://api.github.com/users/pixeltrix/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pixeltrix/subscriptions","organizations_url":"https://api.github.com/users/pixeltrix/orgs","repos_url":"https://api.github.com/users/pixeltrix/repos","events_url":"https://api.github.com/users/pixeltrix/events{/privacy}","received_events_url":"https://api.github.com/users/pixeltrix/received_events","type":"User","site_admin":false},"created_at":"2015-07-28T18:56:41Z","updated_at":"2015-07-28T18:56:41Z","body":"Okay, I've worked out why there's a difference - it's because of the option `belongs_to_required_by_default`. Changing this from true to false makes the tests work, however I'm not sure why the `require: true` option on the `belongs_to` associations for the join model are failing because both models do exist. I'll keep digging.\r\n\r\n"}}