rubyonjets / jets

Ruby on Jets
http://rubyonjets.com
MIT License
2.6k stars 181 forks source link

jets deploy: undefined local variable or method `searchkick' for ModelName #245

Closed noelrocha closed 5 years ago

noelrocha commented 5 years ago

Hi! Thanks for this awesome framework. Its really helping my company to go microservices! But Im getting an weird error when adding ElasticSearch support using the searchkick gem.

Checklist

My Environment

Software Version
Operating System macOS Mojave 10.14.3
Jets 1.8.11
Ruby 2.5.3

Expected Behaviour

It should successfully deploy my application

Current Behavior

Its giving me the following error: 'method_missing': undefined local variable or methodsearchkick' for ModelName`

Step-by-step reproduction instructions

Example: echo "class MyModel < ApplicationRecord\n searchkick \n end" > app/models/my_model.rb jets deploy

the jets console works like a charm, I can search on the ElasticSearch without problems.

Code Sample

Traceback (most recent call last):
    34: from /Users/noelrocha/.rvm/gems/ruby-2.5.3/bin/ruby_executable_hooks:24:in `<main>'
    33: from /Users/noelrocha/.rvm/gems/ruby-2.5.3/bin/ruby_executable_hooks:24:in `eval'
    32: from /Users/noelrocha/.rvm/gems/ruby-2.5.3/bin/jets:23:in `<main>'
    31: from /Users/noelrocha/.rvm/gems/ruby-2.5.3/bin/jets:23:in `load'
    30: from /Users/noelrocha/.rvm/gems/ruby-2.5.3/gems/jets-1.8.11/exe/jets:14:in `<top (required)>'
    29: from /Users/noelrocha/.rvm/gems/ruby-2.5.3/gems/jets-1.8.11/lib/jets/cli.rb:5:in `start'
    28: from /Users/noelrocha/.rvm/gems/ruby-2.5.3/gems/jets-1.8.11/lib/jets/cli.rb:20:in `start'
    27: from /Users/noelrocha/.rvm/gems/ruby-2.5.3/gems/jets-1.8.11/lib/jets/cli.rb:48:in `boot_jets'
    26: from /Users/noelrocha/.rvm/gems/ruby-2.5.3/gems/jets-1.8.11/lib/jets/core.rb:18:in `boot'
    25: from /Users/noelrocha/.rvm/gems/ruby-2.5.3/gems/jets-1.8.11/lib/jets/booter.rb:24:in `boot!'
    24: from /Users/noelrocha/.rvm/gems/ruby-2.5.3/gems/jets-1.8.11/lib/jets/booter.rb:201:in `eager_load_app'
    23: from /Users/noelrocha/.rvm/gems/ruby-2.5.3/gems/jets-1.8.11/lib/jets/booter.rb:201:in `select'
    22: from /Users/noelrocha/.rvm/gems/ruby-2.5.3/gems/jets-1.8.11/lib/jets/booter.rb:219:in `block in eager_load_app'
    21: from /Users/noelrocha/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.3/lib/active_support/core_ext/string/inflections.rb:68:in `constantize'
    20: from /Users/noelrocha/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.3/lib/active_support/inflector/methods.rb:281:in `constantize'
    19: from /Users/noelrocha/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.3/lib/active_support/inflector/methods.rb:281:in `inject'
    18: from /Users/noelrocha/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.3/lib/active_support/inflector/methods.rb:281:in `each'
    17: from /Users/noelrocha/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.3/lib/active_support/inflector/methods.rb:283:in `block in constantize'
    16: from /Users/noelrocha/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.3/lib/active_support/inflector/methods.rb:283:in `const_get'
    15: from /Users/noelrocha/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:195:in `const_missing'
    14: from /Users/noelrocha/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:510:in `load_missing_constant'
    13: from /Users/noelrocha/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:356:in `require_or_load'
    12: from /Users/noelrocha/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:37:in `load_interlock'
    11: from /Users/noelrocha/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.3/lib/active_support/dependencies/interlock.rb:13:in `loading'
    10: from /Users/noelrocha/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.3/lib/active_support/concurrency/share_lock.rb:151:in `exclusive'
     9: from /Users/noelrocha/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.3/lib/active_support/dependencies/interlock.rb:14:in `block in loading'
     8: from /Users/noelrocha/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:37:in `block in load_interlock'
     7: from /Users/noelrocha/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:373:in `block in require_or_load'
     6: from /Users/noelrocha/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:475:in `load_file'
     5: from /Users/noelrocha/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:661:in `new_constants_in'
     4: from /Users/noelrocha/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:476:in `block in load_file'
     3: from /Users/noelrocha/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:476:in `load'
     2: from /Users/noelrocha/Workspace/vai-bi/app/models/invoice.rb:1:in `<top (required)>'
     1: from /Users/noelrocha/Workspace/vai-bi/app/models/invoice.rb:4:in `<class:Invoice>'
/Users/noelrocha/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.3/lib/active_record/dynamic_matchers.rb:22:in `method_missing': undefined local variable or method `searchkick' for Invoice (call 'Invoice.connection' to establish a connection):Class (NameError)

Solution Suggestion

tongueroo commented 5 years ago

Thanks for the kind words. Browsed through the source of searchkick to see if there's any special loading. Doesn't look like it πŸ€” Nevertheless, if we require searchkick explicitly it works. Here are 2 options:

  1. Require it on the model

app/models/post.rb:

require "searchkick"
class Post < ApplicationRecord
  searchkick
end
  1. Require it as an initializer so it's available everywhere

config/initializers/searchkick.rb:

require "searchkick"

Side note: One of the reasons, decided to eager load things as part of the jets deploy is that it helps to find issues before deployment. Feel like these issues are better found early before its running on AWS Lambda.

noelrocha commented 5 years ago

the initializer solution worked great! thanks!

iamrahulroy commented 5 years ago

@tongueroo I was facing similar issues with closure_tree and fast_jsonapi. Loading both gems in initializers solved the issue.

IMHO jets initializers documentation should be updated.

thushw commented 5 years ago

I hit the same issue with Aws::SecretsManager. I need to get the database creds from this inside my database.yml, requiring the module fixed it :

<% require 'aws-sdk-secretsmanager' %>
<% client = Aws::SecretsManager::Client.new(region: 'us-east-1') %>