{"action":"closed","issue":{"url":"https://api.github.com/repos/rails/rails/issues/20633","repository_url":"https://api.github.com/repos/rails/rails","labels_url":"https://api.github.com/repos/rails/rails/issues/20633/labels{/name}","comments_url":"https://api.github.com/repos/rails/rails/issues/20633/comments","events_url":"https://api.github.com/repos/rails/rails/issues/20633/events","html_url":"https://github.com/rails/rails/issues/20633","id":89600063,"number":20633,"title":"Shouldn't nested updates respect the relationship's scope?","user":{"login":"agoodno","id":198522,"avatar_url":"https://avatars.githubusercontent.com/u/198522?v=3","gravatar_id":"","url":"https://api.github.com/users/agoodno","html_url":"https://github.com/agoodno","followers_url":"https://api.github.com/users/agoodno/followers","following_url":"https://api.github.com/users/agoodno/following{/other_user}","gists_url":"https://api.github.com/users/agoodno/gists{/gist_id}","starred_url":"https://api.github.com/users/agoodno/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/agoodno/subscriptions","organizations_url":"https://api.github.com/users/agoodno/orgs","repos_url":"https://api.github.com/users/agoodno/repos","events_url":"https://api.github.com/users/agoodno/events{/privacy}","received_events_url":"https://api.github.com/users/agoodno/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"},{"url":"https://api.github.com/repos/rails/rails/labels/stale","name":"stale","color":"444444"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2015-06-19T16:04:50Z","updated_at":"2016-03-11T15:11:09Z","closed_at":"2016-03-11T15:11:09Z","body":"Here, we have a Report which can contain Assets which might have been deleted (from user's perspective).\r\n\r\n```ruby\r\nclass Report < ActiveRecord::Base\r\n  has_and_belongs_to_many :assets, -> { unscope(where: :is_fake_deleted) }\r\nend\r\n\r\nclass Asset < ActiveRecord::Base\r\n  default_scope { where is_fake_deleted: false }\r\nend\r\n\r\nclass ReportsController < ApplicationController\r\n  def update\r\n    if @report.update(report_params)\r\n      head :no_content\r\n    else\r\n      ...\r\n    end\r\n  end\r\n\r\n  def report_params\r\n    params.require(:report).permit(..., asset_ids: [], ...)\r\n  end\r\nend\r\n```\r\n\r\nSo in the console..\r\n\r\n```ruby\r\n#Find a report with assets\r\nreport = Report.find(1)\r\n\r\n#Correctly, returns all assets, including deleted. Here, say, 3 is deleted.\r\nreport.assets.map(&:ids)\r\n> [1, 2, 3]\r\n```\r\n\r\nThen, in a test (pretend we have the same report data in the database)...\r\n```ruby\r\n#Update action attempts to save exactly the same asset ids back to report fails\r\nput :update, id: 1, report: { ..., asset_ids: [1, 2, 3], ... }\r\n```\r\n\r\n```bash\r\n[\"Couldn't find all Assets with 'id': (1, 2, 3) [WHERE \\\"assets\\\".\\\"is_fake_deleted\\\" = 'f'] (found 2 results, but was looking for 3)\"]\r\nCompleted 404 Not Found in 163ms (Views: 0.8ms | ActiveRecord: 49.0ms)\r\n```\r\n\r\nThe sql that loads the assets just before the update was:\r\n```bash\r\n  Asset Load (0.4ms)  SELECT \"assets\".* FROM \"assets\"  WHERE \"assets\".\"is_fake_deleted\" = 'f' AND \"assets\".\"id\" IN (1, 2, 3)\r\n   (0.2ms)  ROLLBACK TO SAVEPOINT active_record_1\r\n```\r\n\r\nYou can see that it is respecting Report's `has_and_belongs_to_many :assets` scope during the query through the Report object _but not on update_ (and instead uses the default scope). Is this intended or a bug?\r\n"}}