Search code examples
ruby-on-railsruby-on-rails-5

ActiveRecord::Base.connection.without_sticking not working


we have a rails application which we have upgraded from rails 4 to rails 5 (5.0.7.2) and see that ActiveRecord::Base.connection.without_sticking is no longer working.

and we see the error which I think might be related to makara gem

NoMethodError (undefined method `without_sticking' for #<ActiveRecord::ConnectionAdapters::Mysql2Adapter:0x00007f7c92b9fa80>):
 ActiveRecord::Base.connection.without_sticking do
        find_by_sql(sql)
 end

Any help on how to fix would be really great. Thanks.


Solution

  • I don't know how it used to work in rails 4, but here is how it works in rails 5:

    rbenv shell 2.7.6 
    gem install rails --version 5.0.7.2
    rails _5.0.7.2_ new rails_five_zero -d mysql  
    cd rails_five_zero
    rbenv local 2.7.6
    bundle add makara   # v0.5.0
    
    # docker-compose.yml
    services:
      mysql:
        image: mysql:5.7
        restart: always
        environment:
          MYSQL_DATABASE: "rails_five_zero_development"
          MYSQL_ROOT_PASSWORD: "mypassword"
        ports:
          - 3306:3306
    

    DB configuration has been like this since v0.2.0.beta3, 2013:

    # config/database.yml
    default: &default
      adapter: mysql2
      encoding: utf8
      pool: 5
      username: root
      password: mypassword
      host: 127.0.0.1
    
    development:
      <<: *default
      database: rails_five_zero_development
    
      # NOTE: make sure to specify makara adapter
      #       if you put it here, it will only be available in development
      adapter: 'mysql2_makara'
      makara:
        connections:
          - role: master
            host: 127.0.0.1
    # ...
    

    https://github.com/instacart/makara/tree/v0.5.0#databaseyml

    docker compose up -d
    bin/rails c
    
    >> ActiveRecord::Base.connection.adapter_name
    => "Mysql2"
    
    >> ActiveRecord::Base.connection.class
    => ActiveRecord::ConnectionAdapters::MakaraMysql2Adapter
    # NOTE: this has to be makara adapter^^^^^^^^^^^^^^^^^^^
    
    >> ActiveRecord::Base.connection.method :without_sticking
    => #<Method: ActiveRecord::ConnectionAdapters::MakaraMysql2Adapter(Makara::Proxy)#without_sticking() /home/alex/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/makara-0.5.0/lib/makara/proxy.rb:74>
    
    ?> ActiveRecord::Base.connection.without_sticking do
    ?>   "don't know what to do here"
    >> end
    => "don't know what to do here"
    

    If you don't have makara: config it doesn't work:

    RAILS_ENV=test bin/rails db:create
    RAILS_ENV=test bin/rails c
    
    >> ActiveRecord::Base.connection.class
    => ActiveRecord::ConnectionAdapters::Mysql2Adapter
    #                                    ^
    
    >> ActiveRecord::Base.connection.without_sticking
    NoMethodError (undefined method `without_sticking' for #<ActiveRecord::ConnectionAdapters::Mysql2Adapter:0x000055a3fbdf76a0>)