megahra
megahra

Reputation: 335

Rails Service never called

I've written a simple service to create the data I want to send over my ActionCable connection, but judging from the development log it looks like it is never executed and I can't figure out why.

#app\channels\quiz_data_channel.rb:
class QuizDataChannel < ApplicationCable::Channel
  def subscribed
    stream_from specific_channel
  end

  def send_data
    logger.debug "[AC] send data method entered" #<-- this part is executed

    QuizDataCreation.new(user: current_user).create  #<-- calling the service HERE

    logger.debug "[AC] service created new quiz data"  #<-- not executed

    #after I fix this issue I would broadcast the data here
  end

  private
  def specific_channel
    "quiz_data_#{params[:access_key]}"
  end
end

#app\services\quiz_data_creation.rb:
module Services
  class QuizDataCreation

    def initialize(user)
      self.user = user
      logger.debug "[AC] service - initialize user: #{self.user.inspect}"  #<-- not executed
    end

    def create
      logger.debug "[AC] Service entered!"  #<-- not executed
    end
  end
end

Calling the send_data method works, and the first text ("[AC] send data method entered") is printed into the development log, but that's it. I've been looking at several tutorials and tried placing the service in a subfolder of models at first, and calling Services::QuizDataCreation.new(user: current_user).create , but haven't gotten it to work yet. I'm sure there is a pretty obvious solution, but I just can't see it so I would be really thankful for any pointers.

Upvotes: 0

Views: 86

Answers (1)

Gokul
Gokul

Reputation: 3231

Try defining the service class without module names Services as below:

#app\services\quiz_data_creation.rb:
class QuizDataCreation
  def initialize(user)
    self.user = user
    logger.debug "[AC] service - initialize user: #{self.user.inspect}"  
  end

  def create
    logger.debug "[AC] Service entered!"  
  end
end

And then, you can access it normally as:

QuizDataCreation.new(current_user).create

Upvotes: 1

Related Questions