0

When trying to implement pessimistic locking on a Rails model, I'm not able to do so because HTTParty#lock gets called rather than the ActiveRecord method with the same name.

I'm using AR 6.1.4.1 and HTTParty 0.20.0.

User.lock.find(1)
ArgumentError: wrong number of arguments (given 0, expected 1..2) from /Users/elliottg/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/httparty-0.20.0/lib/httparty.rb:587:in lock

Looks like the HTTParty code that's clobbering the AR method is within the HTTParty module line 587 as shown below.

def lock(path, options = {}, &block) perform_request Net::HTTP::Lock, path, options, &block end

I love HTTParty and want to keep using it.

Any ideas for a solution, other than forking? Thanks!

  • 3
    Can you show where and how you are using HTTParty in your application i.e. is it included in the `User` model. If so, which seems likely in this instance, I would highly recommend abstracting this logic to a Service Object that can interact with both the `User` class and the required `HTTParty` functionality rather than trying to meld the 2 into one Object. – engineersmnky Nov 02 '21 at 15:27
  • 2
    You nailed it! For some reason, I had ```include HTTParty``` on User. Problem solved. Thanks @engineersmnky – Elliott Golden Nov 02 '21 at 15:46

0 Answers0