0

I am trying to call 'checking' action from post model.

class Post < ApplicationRecord
     after_commit :testing
     def testing
         @id = "#{self.id}"
         puts @id
         checking # call action to controller 
     end
end

posts_controller.rb

def checking
  puts "not Working"
end

I am trying to achieve the above, whereas it is not getting called.

raja
  • 15
  • 7
  • you should be calling model functions from controller, models should not be aware of controller logics – Subash Feb 13 '18 at 10:22
  • 1
    What is the reason your doing that? Explain more what are really what to do, because it seems that you are just lost. – oj5th Feb 13 '18 at 10:27
  • The models should not know about the controller logic. Maybe you can refactor and set some variables from controller to model like it is described in this [thread](https://stackoverflow.com/a/2420015/1108032) – mmsilviu Feb 13 '18 at 12:07
  • What exactly you want? Why you exactly need to call a controller? – Manishh Feb 13 '18 at 12:20

4 Answers4

0

You can do this by instantiating the controller.

SomeController.new.some_action

However it is Not Recommended as your model logic should be independent of your controller logic. Your model should follow Single-responsibility Principle.

A class should have only a single responsibility.

You might need some common logic for controller and models or some service!!!!

Manishh
  • 1,444
  • 13
  • 23
0

As it was said: the models should not know about controller.

Depending on the reason,

  1. you can define checking in model and from Controller just call this method for current Post.
  2. if something should be set from Controller and checked in Model , you can use the approach defined here
mmsilviu
  • 1,211
  • 15
  • 25
0

As many (or all) other said, you shouldn't call a Controller action from a Model. If you are saving/destroying and object from a Controller and you want to check is commit was called, you can do it in the controller, after saving or destroying.

posts_controller.rb

def create
  @post = current_user.posts.build(post_params)
  if @post.save
    checking
  end
end

def checking
  puts "Is is working"
end

If this is not what you want, because you are specifically interested in the commit callback, you can change the code to this:

posts_controller.rb

def create
  @post = current_user.posts.build(post_params)
  @post.save
  if @post.commmited?
    checking
  end
end

def checking
  puts "Is is working"
end

And add some logic to your Model:

class Post < ApplicationRecord
  attr_accessor :commit_performed

  #unset the commit_performed attribute on first callbacks
  before_destroy :unset_commit
  before_validation :unset_commit

  #set the commit_performed attribute after commit
  after_commit :set_commit

  def unset_commit
    @commit_performed = false
  end

  def set_commit
    @commit_performed = true
  end

  def commited?
    @commit_performed
  end
end
Pablo
  • 3,004
  • 1
  • 12
  • 19
-1

You should not be calling a controller action from a model. That's not how even controller methods are called. If you want to trigger an action, it should be either written as a target of a view form or input method, or as a redirect through another controller method itself. If you really want this action to be triggered (if you want the message after a record is saved to a listener url), I'd suggest using a lib such as HTTP::Net or a gem like HTTParty to trigger a call to the action, with its url or rails url_helper. That too, is not suggested, and/or is not the way to operate things in rails.

Akash Srivastava
  • 151
  • 1
  • 10