MongoDB with PostgreSQL in One Rails App - ruby-on-rails

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.

Related

Neography and Rails

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)

How to store user generated content in Rails app

I want to create an website where users can create their own teaching resources, e.g. blocks of text with embedded images etc.
How should I store this in a database in rails? I've heard mongoDB is good for storing documents but I was planning on using postgresql for the user database etc. and have read that generally you shouldn't mix different types of db
I'm sure this is an obvious question but I couldn't find an answer anywhere...
Thanks,
Graham
There are several things you could do.
1. Use PostgreSQL for both the Users table and the TeachingResources table. You could simply use a content column of type text to save all the data.
2. Use PostgreSQL but use the HStore functionality to basically store a hash of objects of your choosing, this gives you more flexibility. Rails 4 will support this by default, but there is also a gem you can use.
3. Use a combination of PostgreSQL and MongoDB (or any other NoSQL solution) in your app. I don't see this as a bad solution, but it does put you outside of the "new user constraints" in Rails, so this might not be the best route to start with
4. Go NoSQL all the way. There is no reason you shouldn't be able to use MongoDB for your User model. However, you are right that this type of datastorage can not give full ACID guarantees, so be careful with product planning and know it's vulnerabilities (but also its strengths).

Moving from SQLite3 to Mongo on Heroku?

I'm currently using SQLite3 with a simple post and image sharing app, similar to the Rails 3 Hartl tutorial (in terms of db structure). But I'd like to move to Mongo for future scalability/learning.
I'm also hosted on Heroku, and am using a 15 GB shared db. I attempted to install MongoHQ and MongoMapper (as per Heroku's instructions) for the transition and this part according to Heroku's support is set up correctly. However, when I turn off the shared db, the app stops working, rather than running off of Mongo.
I'm not sure what do do next, do I have to rewrite my code in mongo or does mongo mapper solve all that? Do I lose my data if I change, if so, how do I copy?
Could any of you please point me to some resources or help me out? Thank you very much!!
MongoDB is not a drop in replacement for a SQL database. There are a couple of things you need to adapt:
The models' code are to be updated to use MongoDB. I can suggest using Mongoid, an ODM, as it will ease your learning path. Mongoid implements Active Record.
The current data saved in your SQL database needs to be migrated - and this is not automatic – to MongoDB schemas. MongoDB do not support migrations as you are used to in SQL world. You will need to write your own scripts for that.
I suggest you write a simple app from scratch using your MongoDB ODM of choice – MongoMapper or Mongoid – so that you get familiar with the basis of MongoDB before attempting to make a migration.

using postgreSQL and jsondb in the same web app

My site is written in ruby (rails) and it's very easy to persist the results of an offsite json feed with using jsondb, so I have an app that would benefit from this, but I think I'd like to keep the rest of the site running on postgrs
Would I be better off moving everything to one database (jsondb?) or does rails easily allow me to use multiple ORM's in the same app
# just notes, ignore if you like cos the answers are subjective
# Perhaps I should build two web apps?
Sometimes it is practical to use multiple databases.
I'd take a hard look at the tenacity gem, which was introduced recently as a way to manage multiple databases within Rails .. and even relationships between them.
It doesn't look like it currently supports jsondb, but given its architecture, it should be possible to write your own adapter (... and then contribute it back?)
It's really better to have only one database. If it's better to use postGresql use it or only jsondb.
Having more than one database can be complicated to understand where is really your data.

How should I go about using a rdbms and mongodb in a rails app?

I'm currently testing the waters with mongoid and have so far begun on an ecommerce store. Now of course mongoid doesn't have transactions so I'd like to ideally use mongoid for most of the app including authentication, authorization, product information etc.
However, the lack of transactions necessitate a return to an rdbms. The rdbms would be used purely to record financial transactions.
Is this possible in rails and has anyone done it?
I have limited experience with rails in general but I imagine having the secure part mounted as a engine and urls scoped under secure.myapp.com or myapp.com/secure/ and the user would be redirected to the ssl while rack takes care of things like shared sessions.
Would this work? Or has anyone found a better way of implementing this?
It is possible to mix mongoDB and a traditional RDMS, but you may have to do some extra coding on your part if you want ActiveRecord objects to communicate with MongoDB objects, since the ORMs are different. Keep in mind that while it is true that MongoDB does not support transactions across multiple documents, it does support 'transactional' atomic updates - which means that if all the data you are updating is contained within a single document you don't have to worry about transactions. MongoDB also supports safe updates, allowing you to verify that data has been written to n different replica servers and has been persisted to disk.
As for shared sessions between HTTPS and HTTP - this is not something you have to worry about. You'll define your session store as either MongoDB, MySQL, Memcached or, my recommendation, Cookies. As long as you define your domain as '.myapp.com' the cookies will be shared across all subdomains of your application regardless of the protocol.
While I can't comment directly on the rails aspect of the question, as with the first poster's response, MongoDB does support transactional updates. It's probably simpler to implement your entire system in Mongo, or in an RDBMS.
The real question is what is the motivation behind using mongo here? What are you hoping to gain from a document database model? Do you just want to rip RoR objects directly to mongo?
Just a suggestion, (abstractly) but you could just strictly define your objects up front, and represent that definition in your RDBMS. It will probably save you a lot of time if you don't have a clear motivation for using Mongo. Mongo is an awesome technology, but it's best for sorting through data and cataloging data, rather representing strict data structures (not that it's incapable of doing so, necessarily, but with a document database, you have a lot more flexibility with the content of each object within your db).
Good luck!

Resources