Gem works in IRB but not Rails console - ruby-on-rails

I am trying to use the Hubspot API Ruby gem located here https://github.com/HubSpot/hubspot-api-ruby#installation, but I am having issues with Ruby on Rails.
For all of my other gems, I can just simply add the gem in the Gemfile and it works within the application, but not this case and I cannot figure out why.
For example, here's what I have in my Gemfile:
# HubSpot API Client
gem 'hubspot-api-client'
I have ran bundle install and it installed successfully, but I still can't use that gem. If I load the gem via the irb console, then it works just fine.
Here's what I mean:
Using IRB
root#8af3866a29c8:/myapp# irb
irb(main):001:0> require 'hubspot-api-client'
=> true
irb(main):002:0> Hubspot
=> Hubspot
Using Rails Console
root#8af3866a29c8:/myapp# rails c
Running via Spring preloader in process 6589
Loading development environment (Rails 5.2.4)
irb(main):001:0> require 'hubspot-api-client'
Traceback (most recent call last):
1: from (irb):1
LoadError (cannot load such file -- hubspot-api-client)
irb(main):001:0> Hubspot
Traceback (most recent call last):
1: from (irb):1
NameError (uninitialized constant Hubspot)
irb(main):002:0>
Why does this happen and how do I fix this?

You have to include gem 'hubspot-api-client' in your Gemfile.
Rails only load the gems which are specified in your Gemfile.

Related

cannot load such file -- aws-sdk-elastictranscoder>

I am currently referencing code from the AWS Documentation Ruby Example JobStatusNotificationSample.rb
My error comes from the first line: require 'aws-sdk-elastictranscoder'
Which says that I am not able to load this file. In my code, I am already using
require 'aws-sdk-s3'
require 'aws-sdk-sqs'
with no previous issues. I have tried to add in require 'aws-sdk-core' but that did not change the error.
It is my first time working with an AWS implementation on Ruby.
I have an error below when I m using Ruby SDK gem v3.
Traceback (most recent call last):
2: from test_sdk.rb:1:in `<main>'
1: from /home/xxx/.rbenv/versions/2.7.6/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:83:in `require'
/home/xxx/.rbenv/versions/2.7.6/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:83:in `require': cannot load such file -- aws-sdk-s3 (LoadError)
My solution:
In your terminal install aws-sdk
gem install aws-sdk
Then in your ruby file test_sdk.rb, add these in the top of the file
#!/usr/bin/env ruby
require 'rubygems'
require 'aws-sdk-s3'
Then run ruby test_sdk.rb works for my case.
Reference link:
https://docs.aws.amazon.com/AmazonS3/latest/userguide/UsingTheMPRubyAPI.html

Bundled gem appears in Gemfile.lock but not available in app

I'm looking to use a couple of new gems in my project, 'ruby-fftw3' and 'aubio'. The project has been happily working away and being extended but now every time I try and add a new gem it's unavailable when I try and call it.
The gems are listed in the Gemfile:
gem 'aubio'
gem 'ruby-fftw3'
When I run bundle install they are listed:
Using aubio 0.3.1
...
Using ruby-fftw3 1.0.2
Gemfile.lock also contains both items.
However, when I open rails c and try to use the gem they're unavailable:
irb(main):001:0> require 'ruby-fftw3'
Traceback (most recent call last):
1: from (irb):1
LoadError (cannot load such file -- ruby-fftw3)
irb(main):002:0> Aubio
Traceback (most recent call last):
1: from (irb):2
NameError (uninitialized constant Aubio)
It also appears that Bundler.require(:default).collect(&:name) does not contain either gem!
I've gone through the Gemfile and can't see any formatting errors and if there were any I don't think bundle install would create a lock file which included them.

Ruby require - cannot load such file - my own gem

I've pushed gem into rubygems. I can install it, but I can't require it.
irb(main):032:0> require 'jsql'
Traceback (most recent call last):
4: from C:/Ruby25-x64/bin/irb.cmd:19:in `<main>'
3: from (irb):32
2: from C:/Ruby25-x64/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:122:in `require'
1: from C:/Ruby25-x64/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:122:in `require'
LoadError (cannot load such file -- jsql)
I've had this problem once, with mechanize gem.
It seems you've create a gem called jsql which is listed on https://rubygems.org/gems/jsql.
So in orer to use it, you need to install it, either with gem install jsql or add it to the Gemfile of you project, along with other dependencies, and run bundle install
Only after this procedures, your gem will be available for use.
This generally occurs when the gem isn't installed for the ruby version you're using.
It happened to me with the irb gem, so I ran
RBENV_VERSION=2.6.3 gem install irb
I found that helpful note here. But the same could apply for any gem, just check the ruby version with ruby --version and try the above code (replacing irb with whatever gem you want to install)

Gem can be required in IRB but not by Rails

When I add
require 'soundcloud'
in Rails and start the server using
rails server
I get
...rb:9:in `require': cannot load such file -- soundcloud (LoadError)
the same yields
=> true
in IRB.
I installed Ruby, Rails, etc following http://installrails.com/.
Any ideas what could be causing this?
Why don't you use Bundler?
Add to Gemfile:
gem 'soundcloud'
then run
bundle install
in the root of your project and it all should work automatically.

Rails/Ruby/Postgres - LoadError cannot load such file -- pg_ext

I am trying to call a Ruby script (which connects to a postgres db) using the rails controller below, however it appears it is having problems loading one of the PG gem files. I have set my require statement to require 'pg' and tried the absolute path as well (require /usr/local/rvm/gems/ruby-1.9.3-p194#railsTest/gems/pg-0.14.0/lib/pg/). The file 'pg_ext' is in fact present in the directory. Additionally, I can run the ruby script standalone without any problems (dbrubyscript.rb), however when rails is added to this equation it craps out with a cannot load such file -- pg_ext error.
Any direction here would be much appreciated as I have not been able to find anything online that fixes this issue
Rails controller:
class TestdlController < ApplicationController
def runmyscript
load "/usr/local/rvm/my_app/ruby_scripts/reports/dbrubyscript.rb"
send_file '/usr/local/rvm/tmp/failedtests.csv', :type => 'text/csv', :disposition => 'inline'
flash[:notice] = "Reports are being processed..."
end
end
.rb file (dbrubyscript.rb) has the following:
require 'rubygems'
require 'pg'
connects to (production) database
#conn = PGconn.connect("zzzzz.test.prod", 5432,"","","yyyyy_prod" ,"postgres", "xxxxxx")
.....
Trace Error:
LoadError in TestdlController#runmyscript
cannot load such file -- pg_ext
Rails.root: /usr/local/rvm/my_app Application Trace | Framework Trace
| Full Trace app/controllers/Testdl_controller.rb:3:in `runmyscript'
This error occurred while loading the following files:
/usr/local/rvm/my_app/ruby_scripts/reports/dbrubyscript.rb
/usr/local/rvm/gems/ruby-1.9.3-p194#railsTest/gems/pg-0.14.0/lib/pg/
pg_ext
Try running ruby /usr/local/rvm/gems/ruby-1.9.3-p194#railsTest/gems/pg-0.14.0/lib/pg/ext/extconf.rb and see what errors you get. That helped me determine that in my case, my PostgreSQL client was too old. Your error may be different since you appear to have a current-ish versioninstalled.
I had the same problem.
I have a stand alone ruby script. It connects via pg to postgres and worked if I ran it directly from the shell.
If I tried to run it via rspec I get the Error cannot load such file -- pg.
Solved: The problem for rspec was, the pg gem was not defined in the Gemfile. After put pg into Gemfile, and retestet via rspec, it worked.
Try adding a line to your Gemfile:
gem "pg"
Then run bundler via the command line:
bundle install
Rails uses Bundler to manage your gems and dependencies. You can read a bit more about the idea behind Bundler here: http://gembundler.com/v1.2/rationale.html

Resources