I understood that we interact with DB using Active Record.
My question is, if Active Record in rails is a driver?
No it isnt. ActiveRecord is simply an library that adds ORM functionality to your data. It provides you with objects that encapsulate a row in the database table and adds business logic to it. AR also provides you some other excellent ways to smartly handle your data and it relationship, mostly keeping you agnostic from the nitty-gritty details of the different databases out there.
The drivers that are used for the db connectivity are not provided by AR and they are specific to the database that you use. There are some libraries that are specifically included for connecting to different databases like the mysql2 driver for connecting to MySQL, similarly for the other databases.
In short, ActiveRecord isn't only driver. Drivers are gems mysql, pg, sqlite... ActiveRecors is thing like ORM for abstract database driver. wiki
Related
Is it possible to use Elasticsearch as database for a Rails Application?
I have gone through many sites, blogs, and videos to find the answer of this, but was unable to and this being the closest.
I am not sure how can it be done, what goes in the database/config.yml and will the schema be getting generated after migrate?
Yes, of course it is, but you cannot use ActiveRecord ORM, basically you'll have to create your own adapter.
If you want to go quick, I would advise you to create the activerecord models, just like any regular app, then use Searchkick and create mappings from your models.
You need to be aware that if you're not using a database to hold the values you'll need to create a repository to handle the CRUD operations in Elasticsearch.
Another option is to use https://github.com/elastic/elasticsearch-rails, but in both cases you need to have the Rails models.
If you really want to go for ElasticSearch only, in you controllers you need to call your own created repositories to fetch and save the records in ElasticSearch.
No, only these databases: MySQL, PostgreSQL, SQLite are supported if you want to use ActiveRecord, and there are also mappers for Mongo and the like.
There are some mappers and adapters out there though but I wouldn't touch them with a 10 foot barge pole - some things just shouldn't exist in this world.
I am experimenting using Neography with Rails 3 and can't quite understand where to specify DB connections, Model Validations (validates_presence_of) etc. The examples available for Neography does not have one for Rails. Would appreciate any pointers.
I don't think Neography integrates that closely with rails, or has an ActiveRecord mapper. You can find configuration information on their wiki: they don't explicitly say it, but you should copy that config in to initializers/neography.rb.
In regards to Models, specifically in regards to the Neography gem, you're probably best off storing data in normal Postgres/Mysql/sqlite, and using after_create, after_update, after_delete hooks in your models to then keep the neo4j database in sync. If you do it this way, you really only have to store the minimum amount of data needed in Neo4j (e.g. object ids and only the data your actively using for node and vertice graph operations). Then, when you query neo4j, you'd take the resulting object id's and "rehydrate" them by querying your SQL ActiveRecord models
The alternative is to use the Neo4j gem itself which offers ActiveRecord mappers and a much cleaner api. But I assume that you're not doing that because of environment constraints (for example, you want to run on Heroku and use the Neo4j addon)
Can I use MongoDB and a PostgreSQL in one rails app? Specifically I will eventually want to use something like MongoHQ. So far I have failed to get this working in experimentation. And it concerns me that the MongoDB documentation specifically says I have to disable ActiveRecord. Any advice would be appreciated.
You don't need to disable ActiveRecord to use MongoDB. Check out Mongoid and just add the gem plus any models along side any of your existing ActiveRecord models. You should note that MongoHQ is just a hosting service for MongoDB and can be used alongside any Object Document Mapper (ODM).
For further details check http://mongoid.org/en/mongoid/docs/installation.html. Just skip the optional 'Getting Rid of Active Record' step.
On a recent client site I worked with a production system that merged MySQL and MongoDB data with a single Java app. To be honest, it was a nightmare. To join data between the two databases required complex Java data structures and lots of code, which is actually databases do best.
One use-case for a two database system is to have the pure transactional data in the SQL database, and the aggregate the data into MongoDB for reporting etc. In fact this had been the original plan at the client, but along the way the databases became interrelated for transactional data.
The system has become so difficult to maintain that is is planned to be scrapped and replaced with a MongoDB-only solution (using Meteor.js).
Postgres has excellent support for JSON documents via it's jsonb datatype, and it is fully supported under Rails 4.2, out of the box. I have also worked with this and I find it a breeze, and I would recommend this approach.
This allows an easy mix of SQL and NoSQL transactions, eg
select id, blast_results::json#>'{"BlastOutput2","report","results","search","hits"}'
from blast_caches
where id in
(select primer_left_blast_cache_id
from primer3_output_pairs where id in (185423,185422,185421,185420,185419) )
It doesn't offer the full MongoDB data manipulation features, but probably is enough for most needs.
Some useful links here:
http://nandovieira.com/using-postgresql-and-jsonb-with-ruby-on-rails
https://dockyard.com/blog/2014/05/27/avoid-rails-when-generating-json-responses-with-postgresql
There are also reports that it can outperform MongoDB on json:
http://www.slideshare.net/EnterpriseDB/the-nosql-way-in-postgres
Another option would be to move your Rails app entirely to MongoDB, and Rails has very good support for MongoDB.
I would not recommend running two databases, based on personal observations on how it can go bad.
I am implementing a Rails application that needs to aggregate search results from N independent heterogeneous databases. An example use case would be:
User queries "xpto"
The query is submitted to all the databases registered on the system
The results are transformed and combined in a predefined format
User gets the results
The application needs to provide an extensibility point for the introduction of new databases in the system. A database here can be of different kinds - a flat file, SQL database, REST interface to an SQL database, etc.
If I was working in C#/Java, ignoring speed issues, I would define a plug-in management system where each host would have a plug-in that would know how to query and transform the results of the host. New hosts would be easily introduced by defining a new plug-in and configuring the host in the system.
I am new comer to rails and I am looking for either ideas, tools or design patterns that can help me to solve this problem.
My best guess wolud be to write a custom ActiveRecord Adapter that would query all your databases and combine the results.
From the API reference:
Connections are usually created through ActiveRecord::Base.establish_connection and retrieved by ActiveRecord::Base.connection. All classes inheriting from ActiveRecord::Base will use this connection. But you can also set a class-specific connection. For example, if Course is an ActiveRecord::Base, but resides in a different database, you can just say Course.establish_connection and Course and all of its subclasses will use this connection instead.
In the introductory tutorials, it talks about MySQL, Postgre and Sqlite3. Is anything else supported out-of-the-box? Are there any plugins to support other databases?
I'm mainly interested in NoSQL - Cassandra, CouchDB, etc.
Thanks
Activerecord (Rails' default ORM) supports MySQL, Postgres, and sqlite3. If you want to use a different ORM, you have many more choices.
Some links to get you started:
DataMapper, a popular alternative ORM
Sequel, another alternative ORM
Rails wiki article on using CouchDB
Mongoid, for MongoDB
MongoMapper, an alternative for MongoDB
I don't have enough experience with Cassandra to mention anything for it. This frontier has not been fully explored.
The game is likely to change some when Rails 3 comes out soon. Expect a lot of choices in this area to sprout up.
Take a look here and here and this last one(for couch). Also - if you're using JRuby - you can use any database that has a JDBC driver - which is just about every database around there.
Ruby on Rails 3 ( still in beta ) is ORM agnostic. So You can change your ORM. Or even you can not use ORM.
So the limitation is in Ruby, not in Ruby on Rails. in futur.
The ActiveModel gem help you with several librarie for compatibility. So you can even made you backend in File or with your own database.
Here we can see on the source code the list of available DBs: https://github.com/rails/rails/tree/b002141b1ba2eef5d192ec31bec5340f7ecca5e9/activerecord/lib/active_record/connection_adapters which as shqdowbq mentions is PostgreSQL, MySQL and SQLite.
https://www.ruby-toolbox.com/categories/Active_Record_DB_Adapters contains a list of third party adapters for other databases, listed options include:
https://github.com/rails-sqlserver/activerecord-sqlserver-adapter for Microsoft SQL Server
https://github.com/rsim/oracle-enhanced for Oracle
Related question: List of supported databases?