Using Services to vailidar + ruby on Rails

Asked

Viewed 228 times

1

I’m trying to create a service that will check if a Sario exists

class UserService
  def create_user(user_username, user_email)
    if User.find(user_username)
      false
    end
    if User.find(user_email)
      false
    end
    true
  end
end

And I call him on my controller’s desk

def create
  if UserService.create_user(params[:username], params[:email])
    @user = User.new(user_params)
    if @user.save
      render json: @user, status: :created, location: api_user_path(@user)
    end

    render json: "exist", status: :unprocessable_entity

  end

  render json: @user.errors, status: :unprocessable_entity
end

but this giving error in the line where I call

undefined method `create_user' for UserService:Class

Can you help me? I’m new with ruby on Rails and I’ve actually already asked another question about how services work, but then I found some things and tried to start on my own, but I have no idea if what I’m doing is right.

Just to be clear, I know it would work if I put it right into the controller, but I’m trying to learn how to use services in ruby, so I’m doing it this way.

3 answers

2

You can do a refactoring on this service, using 1 query instead of 2 like this:

class UserService
  def self.create_user(user_username, user_email)
    return false if User.where("username =? or email = ?", user_username, email).any?
    true
  end
end

1

Is it possible to put your code on the github to see it completely? You don’t need to use a service to do this just put it in your users_controller.Rb

def create
  @user = User.new(user_params)
  if @user.save
    render json: @user, status: :created, location: api_user_path(@user)
  else
    render json: @user.errors, status: :unprocessable_entity
  end
end

ae on your User Voce model:

class User < ActiveRecord::Base
  validates :email, uniqueness: true
  validates :user_name, uniqueness: true

It is important to remember that uniqueness conditions should be added to your database not only in the application. I hope I’ve helped.

EDIT

Follow a refactor suggestion after seeing your comment.

class UserService
  def self.create_user?(user_username, user_email)
    !User.where('email = ? OR username = ?', user_email, user_username).exists?
  end
end
  • So I’m trying to put in service, to learn how to use the service, no controler to on that works.

0


Good I managed to do after reading several articles and question to many people. Well had some mistake in the way was quite wrong what I was doing.

So to make the service call, I would have two ways or I would have to stop an object or say that my method is of the statical type and that was the solution I approached. Just put a self.<nome do metodo> that ready he is statical.

another mistake was in the way I was doing the searches. In the end it was like this:

My Method Keeps in Control:

def create
    if UserService.create_user(params[:username], params[:email])
      @user = User.new(user_params)
      if @user.save
        render json: @user, status: :created, location: api_user_path(@user)
        return
      end
      render json: @user.errors, status: :unprocessable_entity
      return
    end

    render json: "exit", status: :unprocessable_entity
  end

My service:

class UserService
  def self.create_user(user_username, user_email)
    if User.find_by_username(user_username)
      return false
    end
    if User.find_by_email(user_email)
      return false
    end
    return true
  end
end

Browser other questions tagged

You are not signed in. Login or sign up in order to post.