jviotti
jviotti

Reputation: 18949

Rails Scaffold destroy not working as expected

I was adding uniqueness validation to the model in charge of user registration. The username should be uniqueness. I created a user when testing some days ago, lets call it "example-guy". Then I deleted him from the scaffolding user interface and now when trying to register a new user as "example-guy", it returns that the name has already been taken. So how can I fix this from the DB without reverting to its "birth-state" and modify the controller to actually destroy the table entry?

Or maybe Rails is keeping track of what was writed to DB, even after destruction?

Im using omniauth-identity, the user model:

class User < ActiveRecord::Base
  attr_accessible :name, :provider, :uid, :email
  # This is a class method, callable from SessionsController
  # hence the "User."
  def User.create_with_omniauth(auth)
    user = User.new()
    user.provider = auth["provider"]
    user.uid = auth["uid"]
    user.name = auth["info"]["name"]
    # ADD EMAIL
    user.email = auth["info"]["email"]
    user.save
    return user
  end
end

User controller's destroy function:

  # DELETE /users/1
  # DELETE /users/1.json
  def destroy
    @user = User.find(params[:id])
    @user.destroy

    respond_to do |format|
      format.html { redirect_to users_url }
      format.json { head :no_content }
    end
  end
end

Validations are made trought "Identity" model inherited from omniauth's active record:

class Identity < OmniAuth::Identity::Models::ActiveRecord
  attr_accessible :email, :name, :password_digest, :password, :password_confirmation
  #attr_accessible :email, :name, :password_digest :password, :password confirmation
  validates_presence_of :name
  validates_uniqueness_of :name
  validates_length_of :name, :in => 6..24
  validates_format_of :name, :with => /^[a-z]+$/
  validate :name_blacklist

  validates_uniqueness_of :email, :case_sensitive => false
  validates_format_of :email,
  :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i,
  :message => "doesn't look like a proper email address"
  #validates_presence_of :password, on: :create
  #validates_presence_of :password_confirmation
  validates_length_of :password, :in => 6..24

  def name_blacklist
    unless self.name.blank? then
      case self.name
      when "user", "photo", "photos",
        "application", "sessions",
        "identities", "home"
          self.errors.add(:name, "prohibited")
      end
    end
  end
end

Identities controllar manage registration form sent to omniauth and calling new.html.erb:

class IdentitiesController < ApplicationController
  def new
    @identity = env['omniauth.identity']
  end
end

Thanks in advance.

Upvotes: 0

Views: 561

Answers (1)

Phil Mulligan
Phil Mulligan

Reputation: 36

I'm not entirely sure what caused the problem, but I came across a similar problem but with email addresses with a Devise based user.

I was wanting to re-use the same email on a different user. I changed the original user to a different email, but when I then tried to update the second user with the "now unique" email, it failed the unique validation.

A query for users with that email returned nothing.

It seemed to be cache-related to the uniqueness constraint, as restarting the server, after deleting the email address, fixed the problem.

Upvotes: 2

Related Questions