Q:

before_create rule not working

In my user.rb model I have the following:
    before_create :check_name

    def check_name
        user = User.where(:name => self.name, :place => self.place)
        if user.present?
          p "ccccccccccccccccccccccccccccccccccccccccc"
          @error = "Same combination of name and place are already present"
          return @error
        end
      end

and in my controller:
    def create
        error_flag = false
        @user =User.new(user_params)
        if @user.save
          error_flag = true
          @success = I18n.t('user.success.user_created')
        else
          @error = find_error(@user)
        end

        respond_to do |format|
          format.js {
            render :layout => false,
                   :locals => {
                     :request => error_flag,
                     :user => @user
                   }
          }
        end
  end

My aim was to not create a user if the condition defined in the model is true, however the user is created anyway. Any ideas what I'm doing wrong?

A:

You can do just a simple thing add this in your model:
validates_uniqueness_of :name, :scope => :place

and remove all the code i.e before_create and its method.

