I'm trying to build a simple ruby script that connects to a database and runs some basic queries.
The code is here: https://github.com/mastermindg/rack-activrecord-example
It's not a service - only a script that is run manually to do batch jobs. My problem is that I need to populate the database for testing purposes. I know how to do this in Sinatra and Rails but it's failing as-is:
NoMethodError: undefined method `set' for main:Object
Did you mean? send
/usr/src/app/app.rb:7:in `<top (required)>'
/usr/src/app/Rakefile:2:in `<top (required)>'
I've got the database.yml but I can't tell how to load it since set is failing.
How do I connect to and query a database using ActiveRecord with basic Rack?
1) Add this to your Rakefile after the requires:
DatabaseTasks.database_configuration = YAML.load(File.read(File.join(root, 'config/database.yml')))
2) Remove set :database_file, 'config/database.yml' from your app.rb file (I think set is a sinatra/activerecord method).
Running rake db:create may give you an error on your project now,because you're using json instead of JSON in your app.rb file (depending on your local versions), so. . .
3) Change puts json Resource.select('id', 'name').all to puts JSON Resource.select('id', 'name').all in your app.rb file.
Now rake db:create will throw a database error, but that's an error related to your specific database configuration, make an appropriate adjustment to that (this is off-topic from the original question, so I won't address it further) and your app should run as you desire.
More info:
This gist shows example contents of a Rakefile that you could use to run Active Record tasks without using Rails or Sinatra.
Related
I have hanami 1.3.0 app named booking. There is rake task in /rakelib/motel.rake :
require_relative '../lib/booking' # it requires booking/motel/booker file
namespace :motel do
task :book do
Booking::Motel::Booker.new.book
end
end
booking/motel/booker requires booking_repository file, and tries to instantiate BookingRepository, but fails with error:
NameError: uninitialized constant Hanami::Repository
<root>/lib/booking/repositories/booking_repository.rb:1:in <top (required)>'
However, when I run Booking::Motel::Booker.new.book in hanami console, it loads BookingRepository without problems.
Looked at numerous stack questions regarding hanami rake, but couldn't find an answer.
As it turns out, it was a foolish mistake. I forgot to add :environment to my task.
namespace :motel do
task book: :environment do
Booking::Motel::Booker.new.book
end
end
That fixed it and dropped the need to use manual file loading, of course.
I have been struggling for a while with this problem. I'm trying to test a ruby CLI that uses a lot of fileutils operations like making directories and copying files, etc. But when I'm writing tests, I need some way to test the functionality without touching and changing the actual filesystem. I am trying to use the fakefs gem but I am running into a error right away
When I successfully run `dotfiles init` # aruba-0.5.3/lib/aruba/cucumber.rb:71
No such file or directory - No such file or directory - /tmp/aruba-out20130817-15800-ke8nou (Errno::ENOENT)
/Users/Brian/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/tempfile.rb:146:in `open'
/Users/Brian/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/tempfile.rb:146:in `block in initialize'
/Users/Brian/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/tmpdir.rb:142:in `create'
/Users/Brian/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/tempfile.rb:136:in `initialize'
features/dotfiles_repository.feature:8:in `When I successfully run `dotfiles init`'
So its not finding /tmp/aruba which is where cucumber aruba does all of its file operations according to the readme.
The tests run fine without fakefs, so I'm assuming its something with the setup.
Here is my features/support/env.rb file
require 'aruba/cucumber'
require 'fakefs/safe'
Before do
FakeFS.activate!
end
After do
FakeFS::FileSystem.clear
FakeFS.deactivate!
end
Does anyone have any idea what I may be doing wrong? Any help is appreciated. Even if its an idea that doesn't use fakefs, I would like to hear it. Thanks
I have a sinatra ruby app in heroku. I am trying to access the database via the console.When I run the heroku run console , I am getting the following error.
Running console attached to terminal... up, run.10
/app/vendor/ruby-1.9.2/lib/ruby/1.9.1/irb/init.rb:281:in `require':LoadError: no such file to load -- ./console.
When I try to access the record using the following command, I am getting the following error :
irb(main):001:0> Setting.first
NameError: uninitialized constant Object::Setting
from (irb):1
from bin/irb:12:in `<main>'
Can anyone help me in what needs to be done. Am I missing some file or Is there a different way to access the tables in heroku?
The heroku console thing is an old hack for rails apps, but it won't work elsewhere. As you can see from the output, it's trying to load a file called ./console. So, create a console file on your project root, and invoke IRB from it after having connected to your database. For example:
#!/usr/bin/env ruby
require 'irb'
require 'irb/completion'
require 'rubygems'
require 'bundler/setup'
# require something that connects to your database
# or just connect here using ENV['DATABASE_URL']
require 'your_project_setup'
IRB.start
I'm new to Ruby and trying to find a way to check if a proxy is working. I've tried using the 'net/ping' library but I am just getting some errors while requiring it(in Interactive Ruby the script works even though there are the errors, it just closes itself when I run the script normally though)
I'm trying to find a solution to make it either ignore the error, fix the error or find another way to check if the proxy works.
Thanks for help.
This is the error message I get when I try to require the 'net/ping'
proxy.rb(main):005:0> require 'net/ping'
LoadError: no such file to load -- windows/console
from C:/Ruby191/lib/ruby/gems/1.9.1/gems/net-ping-1.5.2/lib/net/ping/ext
ernal.rb:12:in require'
from C:/Ruby191/lib/ruby/gems/1.9.1/gems/net-ping-1.5.2/lib/net/ping/ext
ernal.rb:12:in'
from C:/Ruby191/lib/ruby/gems/1.9.1/gems/net-ping-1.5.2/lib/net/ping.rb:
10:in require'
from C:/Ruby191/lib/ruby/gems/1.9.1/gems/net-ping-1.5.2/lib/net/ping.rb:
10:in'
from C:\Users\X\Desktop\proxy.rb:5:in require'
from C:\Users\X\Desktop\proxy.rb:5
from C:/Ruby191/bin/irb:12:in'
In my opinion the only way to check if a proxy is working is to connect through it and make a request.
I am trying to run metric_fu on a Rails 3 application. All is good, with the exception of rcov. I have RSpec configured and my tests follow the spec/**/*.rb format and run fine in RSpec. Trying to check coverage with rcov, however, I get the following errors:
** Running the specs/tests in the [test] environment
No file to analyze was found. All the files loaded by rcov matched one of the
following expressions, and were thus ignored:
[/\A\/Users\/Eric\/\.rvm\/rubies\/ruby\-1\.8\.7\-p299\/lib/,
/\btc_[^.]*.rb/,
/_test\.rb\z/,
/\btest\//,
/\bvendor\//,
/\A\/Users\/Eric\/\.rvm\/gems\/ruby\-1\.8\.7\-p299#-backend\/gems\/rcov\-0\.9\.8\/lib\/rcov\/formatters\/base_formatter\.rb\z/,
/\bvendor\//,
/\bconfig\//,
/\benvironment\//,
/\/gems\//,
/\/Library\//,
/\/usr\//,
/spec/]
You can solve this by doing one or more of the following:
* rename the files not to be ignored so they don't match the above regexps
* use --include-file to give a list of patterns for files not to be ignored
* use --exclude-only to give the new list of regexps to match against
* structure your code as follows:
test/test_*.rb for the test cases
lib/**/*.rb for the target source code whose coverage you want
making sure that the test/test_*.rb files are loading from lib/, e.g. by
using the -Ilib command-line argument, adding
$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
to test/test_*.rb, or running rcov via a Rakefile (read the RDoc
documentation or README.rake in the source distribution).
/Users/Eric/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- spec_helper (LoadError)
from /Users/Eric/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from ./spec/models/account_spec.rb:1
from /Users/Eric/.rvm/gems/ruby-1.8.7-p299#backend/gems/rcov-0.9.8/bin/rcov:511:in `load'
from /Users/Eric/.rvm/gems/ruby-1.8.7-p299#backend/gems/rcov-0.9.8/bin/rcov:511
from /Users/Eric/.rvm/gems/ruby-1.8.7-p299#backend/bin/rcov:19:in `load'
from /Users/Eric/.rvm/gems/ruby-1.8.7-p299#backend/bin/rcov:19
When I run rcov directly, it appears that part of the issue is rcov not correctly loading the spec_helper in my specs, which is required as "require 'spec_helper'".
Any help on how to resolve this? Thanks.
Adding "-Ispec" to the Metric-Fu configuration fixes this.