Mongoid: uninitialized constant (NameError) - ruby

I'm currently trying to write a very basic portion of code that reads a ruby file and instantiate some documents into my local MongoDB.
The class modeling my Mongo document is in a separate file called 'search_term.rb', as follows:
class SearchTerm
include Mongoid::Document
field :search_term, type: String
end
In a separate file called 'populate_database.rb' that resides in the same folder as 'search_term.rb', I'm trying to read from a list and create new documents and look into my MongoDB Compass if they are actually being created:
require_relative 'search_term'
business_list = [
'business name 1',
'business name 2',
'business name 3',
'business name 4',
]
for business in business_list
s_term = SearchTerm.new()
s_term.search_term = business
s_term.post
end
The problem is: when I run 'ruby populate_database.rb' I'm getting the error:
<class:SearchTerm>': uninitialized constant SearchTerm::Mongoid (NameError)
I have already tried creating an empty Gemfile and writing:
gem 'mongo'
gem 'mongoid'
into them and running 'bundle install'. After that the following Gemfile.lock file is created:
GEM
specs:
activemodel (6.1.4.1)
activesupport (= 6.1.4.1)
activesupport (6.1.4.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
zeitwerk (~> 2.3)
bson (4.12.1)
concurrent-ruby (1.1.9)
i18n (1.8.10)
concurrent-ruby (~> 1.0)
minitest (5.14.4)
mongo (2.13.0)
bson (>= 4.8.2, < 5.0.0)
mongoid (7.3.2)
activemodel (>= 5.1, < 6.2)
mongo (>= 2.10.5, < 3.0.0)
ruby2_keywords (~> 0.0.5)
ruby2_keywords (0.0.5)
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
zeitwerk (2.4.2)
PLATFORMS
x64-mingw32
DEPENDENCIES
mongo
mongoid
BUNDLED WITH
2.2.27
But the problem still persists. Can you guys give me some advice on how to solve this one?
Thanks in advance!!

If you are not using Rails, you need to require "mongoid" yourself somewhere in your application and then load Mongoid configuration. See here for an example of how to do that.

Related

Why do I get an 'invalid attribute' exception from watir-webdriver method "assert_valid_as_attribute" when passing in ':css'?

I recently inherited a Ruby test suite which is running watir-webdriver (0.6.4), and I'm in the unfortunate situation where the suite runs just fine on my colleague's machine, but several cases error out on mine. I've tried locking every version of all my gems to exactly what's on his machine to try to solve the compatibility issue, but no dice. The core error seems to be that I get the following exception originating from watir's internal `assert_valid_as_attribute' method:
(Watir::Exception::MissingWayOfFindingObjectException: invalid attribute: :css)
The originating call in my code is:
search_results.selectable_cards_plural.take(assets_selected).each { |assets| assets.fire_event ("onclick") }
search_results{...}.take returns an array of divs.
Has anyone seen this issue before? I've seen two other SO posts mentioning this issue, but neither of them seemed to address my particular issue.
I've included a portion of my gemfile.lock below, in case anyone can easily spot the compatibility issue. I'm running Ruby 2.5.0. And yes, I plan to update tests to run newer gem versions in the near future, but I want to have these legacy regression tests running in the meanwhile.
Selectors mentioned above:
def search_results; #browser.div(:class, 'search-results-grid') end
def selectable_cards_plural; divs(:css => 'div.p-card-hover.p-card-selectable:not(.p-card-selected)') end
The element being selected is:
class="p-card-hover p-card-selectable css-g6ribn-StyledCardHover-visible-visible-visible-visible-StyledCardHover erkh22a0"
Environment info:
MacOS macOS 10.14.3 (Mojave)
Chrome Version 72.0.3626.121
Browser driver: #browser = Watir::Browser.new :chrome, :prefs => chrome_prefs, switches: %w[--window-size=1600,900 --ignore-certificate-errors --disable-popup-blocking --disable-translate]
Stack trace:
Asset Section: Adding # When selecting specific # from search adds assets to section
Failure/Error: search_results.selectable_cards_plural.take(assets_selected).each { |assets| assets.fire_event ("onclick") }
Watir::Exception::MissingWayOfFindingObjectException:
invalid attribute: :css
# /Users/jnguyen/.rvm/gems/ruby-2.5.0/gems/watir-webdriver-0.6.4/lib/watir-webdriver/locators/element_locator.rb:238:in `assert_valid_as_attribute'
# /Users/jnguyen/.rvm/gems/ruby-2.5.0/gems/watir-webdriver-0.6.4/lib/watir-webdriver/locators/element_locator.rb:219:in `normalize_selector'
# /Users/jnguyen/.rvm/gems/ruby-2.5.0/gems/watir-webdriver-0.6.4/lib/watir-webdriver/locators/element_locator.rb:198:in `block in normalized_selector'
# /Users/jnguyen/.rvm/gems/ruby-2.5.0/gems/watir-webdriver-0.6.4/lib/watir-webdriver/locators/element_locator.rb:195:in `each'
# /Users/jnguyen/.rvm/gems/ruby-2.5.0/gems/watir-webdriver-0.6.4/lib/watir-webdriver/locators/element_locator.rb:195:in `normalized_selector'
# /Users/jnguyen/.rvm/gems/ruby-2.5.0/gems/watir-webdriver-0.6.4/lib/watir-webdriver/locators/element_locator.rb:98:in `find_all_by_multiple'
# /Users/jnguyen/.rvm/gems/ruby-2.5.0/gems/watir-webdriver-0.6.4/lib/watir-webdriver/locators/element_locator.rb:46:in `locate_all'
# /Users/jnguyen/.rvm/gems/ruby-2.5.0/gems/watir-webdriver-0.6.4/lib/watir-webdriver/element_collection.rb:96:in `elements'
# /Users/jnguyen/.rvm/gems/ruby-2.5.0/gems/watir-webdriver-0.6.4/lib/watir-webdriver/element_collection.rb:86:in `to_a'
# /Users/jnguyen/.rvm/gems/ruby-2.5.0/gems/watir-webdriver-0.6.4/lib/watir-webdriver/element_collection.rb:29:in `each'
# ./lib/live_editor/assets_le.rb:37:in `take'
# ./lib/live_editor/assets_le.rb:37:in `select_assets'
# ./spec/smoke/live_editor/standard/asset_add_num_spec.rb:23:in `block (3 levels) in <top (required)>'
Gemfile.lock
{...}
commonwatir (4.0.0)
rake (12.3.2)
rspec (3.5.0)
rspec-core (~> 3.5.0)
rspec-expectations (~> 3.5.0)
rspec-mocks (~> 3.5.0)
rspec-core (3.5.2)
rspec-support (~> 3.5.0)
rspec-expectations (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.5.0)
rspec-mocks (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.5.0)
rspec-support (3.5.0)
rspec_html_formatter (0.3.1)
activesupport (>= 4.1.4)
rouge (>= 1.6.1)
rspec-core (>= 3.0.3)
selenium-webdriver (2.53.4)
childprocess (~> 0.5)
rubyzip (~> 1.0)
websocket (~> 1.0)
watir (5.0.0)
commonwatir (~> 4)
watir-webdriver
watir-rspec (2.0.3)
rspec (~> 3.0)
watir (~> 5.0)
watir-scroll (0.1.0)
watir-webdriver
watir-webdriver (0.6.4)
selenium-webdriver (>= 2.18.0)
PLATFORMS
ruby
DEPENDENCIES
{...}
rake
rspec (= 3.5.0)
rspec-core (= 3.5.2)
rspec_html_formatter (~> 0.3.1)
selenium-webdriver (= 2.53.4)
watir (~> 5.0.0)
watir-rspec (= 2.0.3)
watir-scroll (~> 0.1.0)
watir-webdriver (= 0.6.4)
BUNDLED WITH
1.17.3
There appears to be a version mismatch.
In v0.6.4, the following will thrown the exception:
gem 'selenium-webdriver', '=2.53.4'
gem 'watir-webdriver', '=0.6.7'
require 'watir-webdriver'
browser = Watir::Browser.new :chrome
at_exit { browser.close }
p browser.divs(:css => 'div').count
#=> `assert_valid_as_attribute': invalid attribute: :css (Watir::Exception::MissingWayOfFindingObjectException)
In this version, the only support for the :css locator was when it was by itself. The above example would include the :tag_name locator, which is the reason for the exception. You would have had to use #elements instead of divs.
This was addressed in v0.6.8 (https://github.com/watir/watir/pull/244). I would expect that your code base was created with at least v0.6.8.

Openshift: Cannot find rack-1.5.2 in any of sources

I want to run a small test Sinatra application. Here is the error message:
Could not find rack-1.5.2 in any of the sources (Bundler::GemNotFound)
I have verified that the rack 1.5.2 gem is installed in my environment.
Here is the Gemfile.lock
GEM
remote: https://rubygems.org/
specs:
rack (1.5.2)
sass (3.1.20)
sequel (4.10.0)
sinatra (1.0)
rack (>= 1.0)
slim (2.0.2)
temple (~> 0.6.6)
tilt (>= 1.3.3, < 2.1)
sqlite3 (1.3.6)
temple (0.6.7)
tilt (2.0.1)
PLATFORMS
ruby
DEPENDENCIES
rack
sass (= 3.1.20)
sequel
sinatra
slim (= 2.0.2)
sqlite3 (= 1.3.6)
I would appreciate any help on how to proceed.

Ruby Gem ActiveAdmin Install Error

i am trying to install ActiveAdmin Gem i get the following error. i am running it from the commandline
harsha#harshamv:~$ gem install activeadmin
ERROR: While executing gem ... (Gem::DependencyResolutionError)
conflicting dependencies arel (~> 2.0.2) and arel (~> 4.0.0)
Activated arel-4.0.0 via:
arel-4.0.0 (~> 4.0.0), activerecord-4.0.0.rc2 (= 4.0.0.rc2), rails-4.0.0.rc2 (< 4, >= 3.0.0), activeadmin-0.6.3 (= 0.6.3)
instead of (~> 2.0.2) via:
meta_search-1.0.0 (~> 1.0), activeadmin-0.6.3 (= 0.6.3)
If you are using Rails 4, you need to use >= 1.0.0.pre version of ActiveAdmin. The best way of doing that right now is:
gem 'activeadmin', github: 'gregbell/active_admin'
in your Gemfile.

Rubygems Loading Wrong Version of Gem

I'm working on a Gem.
Despite specifying the version of activemodel, the gem is actually using activemodel 4.0.0.
As of 4.0.0 the constructor only takes a single argument. So when I try and pass in a second argument I get an Argument error:
/Users/me/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-4.0.0/lib/active_record/core.rb:180:in `initialize': wrong number of arguments (2 for 1) (ArgumentError)
Why is Rubygems using the wrong version of ActiveModel?
I have specified the following dependencies in the gemspec:
gem.add_dependency "log4r"
gem.add_dependency "deep_end"
gem.add_dependency "google_drive"
gem.add_development_dependency 'rake'
gem.add_development_dependency 'rspec'
gem.add_development_dependency "activemodel", "3.2.13"
gem.add_development_dependency "activerecord", "3.2.13"
gem.add_development_dependency "activesupport", "3.2.13"
My Gemfile.lock looks like this:
PATH
remote: .
specs:
drive_time (0.0.1)
deep_end
google_drive
log4r
GEM
remote: https://rubygems.org/
specs:
activemodel (3.2.13)
activesupport (= 3.2.13)
builder (~> 3.0.0)
activerecord (3.2.13)
activemodel (= 3.2.13)
activesupport (= 3.2.13)
arel (~> 3.0.2)
tzinfo (~> 0.3.29)
activesupport (3.2.13)
i18n (= 0.6.1)
multi_json (~> 1.0)
arel (3.0.2)
builder (3.0.4)
deep_end (0.0.1)
log4r
diff-lcs (1.2.4)
faraday (0.8.7)
multipart-post (~> 1.1)
google_drive (0.3.6)
nokogiri (>= 1.4.4, != 1.5.2, != 1.5.1)
oauth (>= 0.3.6)
oauth2 (>= 0.5.0)
httpauth (0.2.0)
i18n (0.6.1)
jwt (0.1.8)
multi_json (>= 1.5)
log4r (1.1.10)
mini_portile (0.5.0)
multi_json (1.7.7)
multi_xml (0.5.4)
multipart-post (1.2.0)
nokogiri (1.6.0)
mini_portile (~> 0.5.0)
oauth (0.4.7)
oauth2 (0.9.2)
faraday (~> 0.8)
httpauth (~> 0.2)
jwt (~> 0.1.4)
multi_json (~> 1.0)
multi_xml (~> 0.5)
rack (~> 1.2)
rack (1.5.2)
rake (10.1.0)
rspec (2.13.0)
rspec-core (~> 2.13.0)
rspec-expectations (~> 2.13.0)
rspec-mocks (~> 2.13.0)
rspec-core (2.13.1)
rspec-expectations (2.13.0)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.13.1)
tzinfo (0.3.37)
PLATFORMS
ruby
DEPENDENCIES
activerecord (= 3.2.13)
activesupport (= 3.2.13)
drive_time!
rake
rspec
I also get the following:
$ bundle show activemodel #/Users/pedr/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activemodel-3.2.13
Make sure you are running your code with bundle exec, otherwise ruby will default to using the most recent version of the gem you have installed.
Using bundle exec will disallow loading of any gems not in your bundle and should correct your problem. Check out the manpage for more details.
Hope this helps!
After trying everything I could think of, the following has done the trick (though whether it has permanently cured the issue, only time will tell).
$ gem clean
Uninstall rbenv
Install rbenv
Delete Gemfile.lock
$ bundle

heroku, thin rack 1.1.0, gemfile requires rack 1.0.1

My ror app runs on rails 2.3.5(which requires rack 1.0.1),
thin on heroku activates rack 1.1.0 by default
and throws error:
" You have already activated rack 1.1.0, but your Gemfile requires rack 1.0.1. Consider using bundle exec."
my Gemfile:
source 'http://rubygems.org'
group :test, :development do
gem "rack","1.0.1"
end
gem "rails","2.3.5"
gem "mysql"
gem "rake", "0.8.3"
gem "declarative_authorization", "0.5.2"
gem "searchlogic"
gem "prawn", "0.6.3"
gem "thin","1.2.11"
group :test, :development do
gem "rack","1.0.1"
end
group :development do
# bundler requires these gems in development
# gem "rails-footnotes"
end
group :test do
# bundler requires these gems while running tests
# gem "rspec"
# gem "faker"
end
----------------------
I installed bundler and developed Gemfile.lock :
GEM
remote: http://rubygems.org/
specs:
actionmailer (2.3.5)
actionpack (= 2.3.5)
actionpack (2.3.5)
activesupport (= 2.3.5)
rack (~> 1.0.0)
activerecord (2.3.5)
activesupport (= 2.3.5)
activeresource (2.3.5)
activesupport (= 2.3.5)
activesupport (2.3.5)
daemons (1.1.4)
declarative_authorization (0.5.2)
eventmachine (0.12.10)
mysql (2.8.1)
prawn (0.6.3)
prawn-core (>= 0.6.3, < 0.7)
prawn-format (>= 0.2.3, < 0.3)
prawn-layout (>= 0.3.2, < 0.4)
prawn-security (>= 0.1.1, < 0.2)
prawn-core (0.6.3)
prawn-format (0.2.3)
prawn-core
prawn-layout (0.3.2)
prawn-security (0.1.1)
rack (1.0.1)
rails (2.3.5)
actionmailer (= 2.3.5)
actionpack (= 2.3.5)
activerecord (= 2.3.5)
activeresource (= 2.3.5)
activesupport (= 2.3.5)
rake (>= 0.8.3)
rake (0.8.3)
searchlogic (2.4.28)
activerecord (>= 2.0.0)
thin (1.2.11)
daemons (>= 1.0.9)
eventmachine (>= 0.12.6)
rack (>= 1.0.0)
PLATFORMS
ruby
DEPENDENCIES
declarative_authorization (= 0.5.2)
mysql
prawn (= 0.6.3)
rack (= 1.0.1)
rails (= 2.3.5)
rake (= 0.8.3)
searchlogic
thin (= 1.2.11)
---------------------------
Please help me deploy it on heroku........
Solved it...
my app uses rack 1.0.1 , rails 2.3.5
and heroku activates rake 1.1.0 by default..
so i switched my rails app version to 2.3.11 which uses rack 1.1.2 in environment.rb and created a Gem file,
installed bundler
tested on my laptop...
generated lock file and pushed it to heroku...
finally worked....
took a week to get my app working...
:)

Resources