ActiveRecord multiple databases without Rails - activerecord

I would like to use ActiveRecord in a project without Rails.
I would also like to specify the database connections in config/database.yml like Rails does.
There is already one answer how multiple database connections work with Rails.
ActiveRecord talk to two databases?
I was unsuccessful using this approach and I'm not sure why. I only get the error message "database configuration does not specify adapter".
Is there some kind of magic in Rails that reads the database.yml that is not in ActiveRecord?
I could read it myself but then I don't know how to feed it to ActiveRecord so that I can use establish_connection in each model.

I found out how it works. You just fill the ActiveRecord::Base.configurations hash and then it works.
http://apidock.com/rails/ActiveRecord/Base/configurations/class

Related

How to create empty Postgresql database with Datamapper

I have been learning about databases and wanted to know, if is there a Datamapper method -or another "ruby" way?- for creating an empty database rather than using create database "bd_name"; in psql, in a Sinatra project?
What you should be doing is creating a Rakefile and defining a Rake task to create the database and also a task to run the migrations after you make any structural changes to your database.
With DataMapper, this is somewhat more work than with ActiveRecord (which provides it's own DB tasks).
Have a look at the Rakefile provided by the Padrino framework for inspiration.

How do I do a SQLite to PostgreSQL migration?

I have a problem with migrating my SQLite3 database to PostgreSQL. How and what do I need to do?
I am searching the internet, but find only migrations from MySQL to PostgreSQL.
Can anyone help me?
I need to convert my SQLite database to PostgreSQL database for Heroku cloud hosting.
You don't want to try to do a binary conversion.
Instead, rely on exporting the data, then importing it, or use the query language of both and using selects and inserts.
I HIGHLY recommend you look at Sequel. It's a great ORM, that makes switching between DBMs very easy.
Read through the opening page and you'll get the idea. Follow that by reading through the cheat sheet and the rest of the documentation and you'll quickly see how easy and flexible it is to use.
Read about migrations in Sequel. They're akin to migrations in Rails, and make it very easy to develop a schema and maintain it across various systems.
Sequel makes it easy to open and read the SQLite3 table, and concurrently open a PostgreSQL database and write to it. For instance, this is a slightly modified version of the first two lines of the "cheat sheet":
SQLITE_DB = Sequel.sqlite('my_blog.db')
PGSQL_DB = Sequel.connect('postgres://user:password#localhost/my_db')
Base all your subsequent interactions with either database using SQLITE_DB and PGSQL_DB and you'll be on your way to porting the data.
The author of Sequel is very responsive and is a big fan of PostgreSQL, so the ORM has great integration with all its features.

How to check if record exists in ruby

I'm using Ruby 1.9.3, MS Sql Server and Windows 7 Ultimate
I'd like to know how to check if a certain record is already existing in the database so that I would avoid duplicate records. In rails it would have been easy to just invoke Object.exists? but I'm using Ruby only.
Thanks
Check the source code for exists? in activerecord
It basically checks if there primary key with the id exists already using sql query.
you can use these to write your own helper method.
or else u can also use activerecord gem in ruby too.

Ruby EventMachine with PostgreSQL

I know that for mysql em-mysql exists as an asynchronous interface driver to MySQL and that Active Record, with some modification, can make immediate use of. I believe Sequel has this capability already. I also understand that the pg gem exposes PostgreSQL's native async API.
My question: is there any Ruby ORM that natively interoperates with EventMachine when the backing database is PostgreSQL? If not, what needs to be done to retrofit Sequel to support async PostgreSQL? ActiveRecord?
This looks like it works with ActiveRecord:
https://github.com/mperham/em_postgresql

ActiveRecord::Base establish connection to Redis

How do I use the activerecord gem with Redis?
I see all these examples with models and Redis but I keep getting a "not connected" error when I try to use them..
ActiveRecord does not know how to speak to Redis, only to SQL databases.
To use redis here is a list of gems you can use:
If you want to use redis directly (I think you should): redis-rb
If you really want an ORM: redis-objects

Resources