0

I have a controller which is

confbridges_controller.rb

which has the method of

def confbridge_status

    conditions = Hash.new
    if params[:cid]
      conditions[:cid] = params[:cid]
    end

    confbridge = Confbridge.new
    confbridge.confbridge_status(params)
    @confbridges = Confbridge.find_by_sql(["SELECT c.id, c.confno, c.max_member, (c.max_member - 5) AS additional, (c.max_member - IFNULL(p.active,0)) AS vacant FROM confbridges c LEFT JOIN (SELECT confbridge_id, COUNT(uid) AS active FROM confbridge_participants WHERE active = 1 GROUP BY confbridge_id) AS p ON c.id = p.confbridge_id WHERE cid = ? ORDER BY vacant DESC", conditions[:cid]])
    respond_to do |format|
        format.html { render :html => @confbridges }
        format.text { render :text => @confbridges.pluck(:id).first }
        format.json { render :json => @confbridges }
    end
  end

how can i call the confbridge_status method in my rake file

this is my code in my rake file

circlenum = args[:circle]
circle = Circle.find_by circle_number: circlenum
if circle.present?
  confbridges = Confbridge.new
  confbridges.confbridge_status(args[:circle])
end

thanks :)

LHH
  • 3,233
  • 1
  • 20
  • 27
uno
  • 867
  • 2
  • 14
  • 29
  • check here http://stackoverflow.com/questions/22936245/call-controller-from-rake-task – LHH Feb 09 '16 at 06:23

1 Answers1

0

The simple answer is you don't. The public api of Rails controllers are the methods which are invoked by a HTTP request.

Yes you can instantiate controllers or add class methods to your controllers which you call externally but its a really bad idea since its a quick way to turn your controllers into a junk drawer.

Instead you should move the code you want to share between your controller and rake task into the model layer or service objects.

However there are valid cases where you would want to invoke your controller via HTTP in a rake task - for example to keep a dyno from falling asleep or want the response from your controller. You would use Net::HTTP or any other HTTP lib.

max
  • 96,212
  • 14
  • 104
  • 165