Sinatra heroku database access - ruby

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

Related

Ruby Rake ActiveRecord Migrate

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.

Heroku load error on require?

My Ruby application runs fine on my nitrous.io box, but when I push it to Heroku and it attempts to run a scheduled process, the logs show this error:
2013-12-23T22:37:11.902160+00:00 heroku[scheduler.4283]: State changed from starting to up
2013-12-23T22:37:12.178751+00:00 app[scheduler.4283]: from /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:53:in `require'
2013-12-23T22:37:12.178751+00:00 app[scheduler.4283]: from /app/bin/rbtc:3:in `<main>'
2013-12-23T22:37:12.178751+00:00 app[scheduler.4283]: /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:53:in `require': cannot load such file -- rbtc_arbitrage (LoadError)
2013-12-23T22:37:13.432972+00:00 heroku[scheduler.4283]: Process exited with status 1
2013-12-23T22:37:13.461438+00:00 heroku[scheduler.4283]: State changed from up to complete
This is the code in /app/bin/rbtc:3
#!/usr/bin/env ruby
require 'rbtc_arbitrage'
RbtcArbitrage::CLI.start ARGV
File structure link
I tried changing this to require_relative as in a answer to someone else on Stack Overflow to no avail.
I'm kinda at a loss here. Any help is appreciated!
Please, make sure that this file exists: lib/rbtc_arbitrage.rb which loads other files in your repo like this (syntax is valid if you are using bundler):
require 'rbtc_arbitrage/version'
require 'rbtc_arbitrage/file1'
# .. and so on
Now, adding this file should work alone, but if this does not work, try adding your lib directory to the LOAD PATH in your bin/rbtc file before any require statments, like this:
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
This explicitly tells ruby to add the lib directory to the load path, and should solve your problem.

Sinatra and Activerecord "cannot load such file -- sinatra/activerecord"

I'm following Micheal Herman's post on Sinatra + PostgreSQL + Heroku, put keep running into an error
require 'sinatra/activerecord'
when I try to launch the app ($ ruby app.rb) or create my database ( $ rake db:create_migration NAME=...) I keep receiving the following error:
cannot load such file -- sinatra/activerecord
sinatra-activerecord-1.2.3 is in my gemlist. What am i missing?
it worked for me after changing this line in environments.rb
sqlite:///dev.db
to
sqlite3:///dev.db

How to specify memcache server to Rack::Session::Memcache?

I'm trying to configure my Rack app to use Memcache for sessions with Rack::Session::Memcache
How do I give it the options (such as server, username and password)?
Presently I have
use Rack::Session::Memcache
But I get the error
in `initialize': No memcache servers (RuntimeError)
Heroku has put the config in environment variables
MEMCACHE_PASSWORD:
MEMCACHE_SERVERS:
MEMCACHE_USERNAME:
I know I can get these in Ruby with ENV['MEMCACHE_PASSWORD'] but I don't know how to give them to Rack::Session::Memcache
Edit: or to Rack::Session::Dalli that would be great too https://github.com/mperham/dalli
This config worked for Heroku, Dalli is clever and knows to look in the environment variables
require 'dalli'
require 'rack/session/dalli'
use Rack::Session::Dalli, :cache => Dalli::Client.new
After reading the source code at https://github.com/mperham/dalli/commit/4ac5a99

Ruby Sinatra - connect to mongoDB on mongoHQ failed

This is just for my weekend project/study, I am very new to Sinatra and MongoDB.
I've installed the gems for mongoDB, such as: mongo, mongo_mapper and mongoid.
When I tried connecting to my database on MongoHQ from localhost, it encountered such an error:
Mongo::ConnectionFailure at /
failed to connect to any given host:port
* file: connection.rb
* location: connect
* line: 489
I found a similar thread on SO, but frankly speaking, I don't quite understand the answers...
Here is my code snippet:
require 'rubygems'
require 'sinatra'
require 'mongo'
require 'mongo_mapper'
get '/' do
MongoMapper.connection = Mongo::Connection.new('flame.mongohq.com', 27044)
MongoMapper.database = 'notes'
MongoMapper.database.authenticate('foo', 'bar')
erb :list
end
I took the above code from here, but it seems not working...
Which part is wrong? Is there another way to do this? In the end this test web app will be deployed onto heroku, so I hope the solution can work with both localhost and my heroku server.
Updated:
I just created a minimal code snippet to test the mongodb connection:
require 'rubygems'
require 'mongo'
db = Mongo::Connection.new("flame.mongohq.com", 27044).db("notes")
But still got the error, after timeout:
$ ruby mongodbtest.rb
/Library/Ruby/Gems/1.8/gems/mongo-1.0.8/lib/../lib/mongo/connection.rb:489:in
`connect': failed to connect to any given host:port (Mongo::ConnectionFailure)
from /Library/Ruby/Gems/1.8/gems/mongo-1.0.8/lib/../lib/mongo/connection.rb:137:in
`initialize'
from mongodbtest.rb:4:in `new'
from mongodbtest.rb:4
The hostname and port are according to mongoHQ documentation, so they must be right.
Thanks for the help in advance.
2nd Update:
I just tested the mongodb connection string using terminal:
mongo mongodb://flame.mongohq.com:27044/notes -u foo -p bar
Unfortunately this would get me an connection failed error, honestly, I don't know why...
I use
uri = URI.parse(ENV['MONGOHQ_URL'])
#mongo_connection = Mongo::Connection.from_uri( uri )
#mongo_db = #mongo_connection.db(uri.path.gsub(/^\//, ''))
#mongo_db.authenticate(uri.user, uri.password)
You can look up your mongo url using the heroku config --long command
Just gave this another try, this time, I was using the ip address taken from ping:
So, if I change :
db = Mongo::Connection.new('flame.mongohq.com', 27060).db("notes")
db.authenticate('fake', 'info')
To:
db = Mongo::Connection.new('184.73.224.5', 27060).db("notes")
db.authenticate('fake', 'info')
That will work...
I still don't understand why the domain name approach won't work, but at least I can finish this off :)

Resources