Skip to content

Deleting relationships will skip some destroy callbacks #1172

@ouabing

Description

@ouabing

This issue is a (choose one):

  • Problem/bug report.
  • Feature request.
  • Request for support. Note: Please try to avoid submitting issues for support requests. Use Gitter instead.

Checklist before submitting:

  • I've searched for an existing issue.
  • I've asked my question on Gitter and have not received a satisfactory answer.
  • I've included a complete bug report template. This step helps us and allows us to see the bug without trying to reproduce the problem from your description. It helps you because you will frequently detect if it's a problem specific to your project.
  • The feature I'm asking for is compliant with the JSON:API spec.

Description

Deleting relationships will skip some destroy callbacks (in my case it skips rails counter cache updating), in both 0.10.x and 0.9.x.

I found this line using delete but not destroy, which gives a little performance boost but may skip some callbacks.

Is it possible to give an option to choose between delete and destroy operations when defining relationships?

Test sample here:

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

gemfile(true, ui: ENV['SILENT'] ? Bundler::UI::Silent.new : Bundler::UI::Shell.new) do
  source 'https://rubygems.org'

  gem 'rails', require: false
  gem 'sqlite3', platform: :mri

  gem 'activerecord-jdbcsqlite3-adapter', '52.0', platform: :jruby

  if ENV['JSONAPI_RESOURCES_PATH']
    gem 'jsonapi-resources', path: ENV['JSONAPI_RESOURCES_PATH'], require: false
  else
    gem 'jsonapi-resources', git: 'https://github.com/cerebris/jsonapi-resources', require: false
  end

end

# prepare active_record database
require 'active_record'

class NullLogger < Logger
  def initialize(*_args)
  end

  def add(*_args, &_block)
  end
end

ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = ENV['SILENT'] ? NullLogger.new : Logger.new(STDOUT)
ActiveRecord::Migration.verbose = !ENV['SILENT']

ActiveRecord::Schema.define do
  # Add your schema here
  create_table :users, force: true do |t|
    t.string :name
    t.integer :followees_count, default: 0
    t.integer :followers_count, default: 0
  end

  create_table :followships, force: true do |t|
    t.integer :follower_id
    t.integer :followee_id
  end
end

# create models
class User < ActiveRecord::Base
  has_many :followships,
           class_name: 'Followship', foreign_key: 'followee_id', inverse_of: :followee, dependent: :destroy
  has_many :inverse_followships,
           class_name: 'Followship', foreign_key: 'follower_id', inverse_of: :follower, dependent: :destroy
  has_many :followees, through: :inverse_followships
  has_many :followers, through: :followships
end

class Followship < ActiveRecord::Base
  belongs_to :follower, class_name: 'User', counter_cache: :followees_count
  belongs_to :followee, class_name: 'User', counter_cache: :followers_count
end

# prepare rails app
require 'action_controller/railtie'
# require 'action_view/railtie'
require 'jsonapi-resources'

class ApplicationController < ActionController::Base
end

# prepare jsonapi resources and controllers
class UsersController < ApplicationController
  include JSONAPI::ActsAsResourceController
end

class UserResource < JSONAPI::Resource
  attribute :name
  filter :name
  has_many :followees, class_name: 'User'
end

class TestApp < Rails::Application
  config.root = File.dirname(__FILE__)
  config.logger = ENV['SILENT'] ? NullLogger.new : Logger.new(STDOUT)
  Rails.logger = config.logger

  secrets.secret_token = 'secret_token'
  secrets.secret_key_base = 'secret_key_base'

  config.eager_load = false
end

# initialize app
Rails.application.initialize!

JSONAPI.configure do |config|
  config.json_key_format = :underscored_key
  config.route_format = :underscored_key
end

# draw routes
Rails.application.routes.draw do
  jsonapi_resources :users, only: [:index, :create]
end

# prepare tests
require 'minitest/autorun'
require 'rack/test'

# Replace this with the code necessary to make your test fail.
class BugTest < Minitest::Test
  include Rack::Test::Methods

  def json_api_headers
    {'Accept' => JSONAPI::MEDIA_TYPE, 'CONTENT_TYPE' => JSONAPI::MEDIA_TYPE}
  end

  def test_delete_followee
    user1 = User.create! name: 'user1'
    user2 = User.create! name: 'user2'
    user1.followees << user2
    user1.reload
    user2.reload
    assert user1.followees_count == 1
    assert user2.followers_count == 1
    data = {
      data: [
        type: 'users',
        id: user2.id.to_s
      ]
    }
    delete "/users/#{user1.id}/relationships/followees", data.to_json, json_api_headers
    assert last_response.status, 204
    user1.reload
    user2.reload
    assert user1.followees_count.zero?
    assert user2.followers_count.zero?
  end

  private

  def app
    Rails.application
  end
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions