Rails:dependent :: destroy重新分配后删除记录

时间:2016-06-24 21:30:42

标签: ruby-on-rails ruby devise belongs-to ruby-on-rails-5

我有一个Rails 5应用,用户可以注册为访客User来试用我的服务。每个User(和访客用户)都可以拥有多个App个。这种关系看起来像这样:

# User.rb
has_many :apps, dependent: :destroy

# App.rb
belongs_to :user

当访客用户决定注册我想要的服务时:

  • (1)将访客用户的应用程序重新分配给新的当前用户。
  • (2)删除旧的访客用户(不删除应用程序)。

我遇到的问题是,在删除旧的访客用户之前,即使我将app.user重新分配给我的新current_user(我使用Device gem),该应用也会被删除。

在我的class Users::RegistrationsController < Devise::RegistrationsController我覆盖create这样的动作(相关部分):

# POST /resource
def create
  user_params = params[:user]
  new_user_from_guest = guest_user
  build_resource(sign_up_params)
  resource.save
  yield resource if block_given?
  if resource.persisted?
    if resource.active_for_authentication?
      set_flash_message :notice, :signed_up if is_flashing_format?
      sign_up(resource_name, resource)

      # Reassigns the app
      reasign_app_for_user(new_user_from_guest)

      Mailer.deliver_welcome_message(current_user).deliver_later!
      respond_with resource, location: after_sign_up_path_for(resource)

      # delete the guest user (this also deletes the app)
      delete_guest_user!
    else
      [...]
    end
  else
    [...]
  end
end

# ... my private methods for deleting the current user and reassigning the app's owner.
def reasign_app_for_user(user)
  user.apps.each do |app|
    app.user = current_user
    app.save!
  end
end

# ... this method deletes the app, even if it is called after the reassign method.
def delete_guest_user!
  guest_user(with_retry = false).try(:destroy)
  session[:guest_user_id] = nil
end

关于为什么会发生这种情况或我做错了什么的任何想法?

更新

这是我的user_params变量在提交后的样子:

<ActionController::Parameters {"name"=>"Test user", "email"=>"test@example.com", "password"=>"testing"} permitted: false>

这就是sign_up_params的样子:

{"email"=>"test@example.com", "password"=>"testing", "name"=>"Test"}

1 个答案:

答案 0 :(得分:1)

我会考虑在将应用分配给新用户之前复制应用。这样,对于它是否是相同的应用程序没有混淆。

guest.apps.each do |app|
  current_user.apps << app.dup
end

就像这样。铲运算符确保将新的重复应用保存到current_user对象。

甚至更简单,更紧凑:

current_user.apps = guest.apps.map(&:dup)
current_user.save