Retains the original ActiveRecord behaviour for #persisted? ... #82

Merged
merged 1 commit into from Nov 28, 2012

4 participants

@chuckbergeron

... So you can still build routes with deleted instances. For example:

paranoid = Paranoid.with_deleted.first
edit_paranoid_path( paranoid ))

(Ignore the strange diff on core.rb, the only important lines are 84 - 87. The rest is just my text editor trying to fix certain lines).

Charles Bergeron Retains the original ActiveRecord behaviour for #persisted? so you ca…
…n still build routes with deleted instances (eg. edit_paranoid_path( paranoid ))
6d37972
@goncalossilva

Sorry, I totally forgot about this. Looks good, thanks!

@goncalossilva goncalossilva merged commit aa0657a into goncalossilva:rails3.2 Nov 28, 2012
@delwyn

This seems to break respond_with behavior in controllers:

class UsersController
  def destroy
    @user = User.find(params[:id])
    @user.destroy
    respond_with(@user)
  end
end

As the record is now persisted after it has been flagged as deleted, it will redirect to that record which will raise ActiveRecord::RecordNotFound

Is this the intended behaviour?

I realize that you can avoid this by specifying the location or redirecting.

Thanks

@chuckg
Collaborator

@delwyn:

Best guess is that respond_with and it's subsequent methods query your User model in some simple way (like find), thereby using the default scope which is hiding your protected deletions; the proper behavior, I'd say. You should be able to avoid any this type of problem with something like:

  User.class.with_scope(User.with_deleted) do
    respond_with(@user)
  end

Out of curiosity, why are responding back with the deleted resource?

@delwyn

@chuckg I always use respond_with as it automatically sets your response status and keeps your controllers dry. It is also the default in the rails scaffold generators.

Before this change

User.last.destroy.persisted? # false

After this change

User.last.destroy.persisted? # true

Just wondering if a deleted record should be persisted?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment