Accessing rails model through rails console

222 Views Asked by At

I was looking to test a method I had created in my "Herd" model. The methods called "safely_assign_herd_usages". I understand how to instantiate a herd e.g. H = Herd.last However, I'm struggling to gain access to the method to test the lines of code within it. Any pointers or help would be greatly appreciated. Thanks.

1

There are 1 best solutions below

0
Chiperific On

Use a debugger in the method you want to test.

debug is the standard option.

require 'debug'

def safely_assign_herd_usages
  ...
  binding.break # will interrupt here
  ...
end

Then in your Rails console, trigger the method to get to the breakpoint:

$ Herd.first.safely_assign_herd_usages

Now the console will load up the method in question and give you an interactive console at that specific spot.