Store "schemas" to build custom forms using postgres and rails - ruby-on-rails

I'm building a site using rails that will have 1000s of custom forms. Before we build a form builder user interface, we'll be essentially creating and storing the "schema" manually and using the "schema" to build the form.
I had originally posted this question to help figure out how to create the form from a YAML file: Create a form in rails by reading a yaml file
But after doing some research, it seems like it would be better to store the "schema" in our db and now I'm wondering what the best way is to do that. It seems like there's a "jsonb" type for postgres dbs that I could use - is that better than storing the "schema" as YAML data? And would I use the "bytea" type for this type of data? Are there any thoughts on if it would be better to use JSON or YAML? Also, do I just put the contents of the schema in the db or is there a way to just store a file?
Thanks!

I would use the native Postgres JSON type.
http://www.postgresql.org/docs/devel/static/datatype-json.html

Related

Schemaless design for Ember data in Ember.js

We are having schemaless database on MongoDB in Rails. I want to know how to use such a schemaless emberjs data design to integrate with such a design.
I am not able to find any documentation on if this can be achieved or if there is any best practice
Can any one help me with this ?
EDIT:
Question is on how to use EmberJS persistent datastore for a schemaless MongoDB Rails backend.
The first two links below should help. See how the first link is setting primaryKey: '_id' to recognize mongodb _id and the second link shows how to make active_model_serializer gem work with mongoid, the gem is ember-core team's recommended gem for working with rails and ember-data :
https://github.com/dagda1/workoutzenith/tree/master/app/models
https://github.com/dagda1/workoutzenith/blob/master/config/initializers/active_model_serializer_mongoid_initializer.rb
If you run into problems with embedded mongodb documents and ember-data, this should solve it:
Ember-data embedded objects stored as separate objects
Read the link below and also examine the github project:
http://tardate.blogspot.co.uk/2012/03/rails-ember-mongodb-bootstrap.html
https://github.com/evendis/rails-ember-mongo-bootstrap-demo
This is not using rails but nodejs, mongodb and emberjs. The mongodb + emberjs might still be useful to you
https://github.com/abelaska/nodejs-emberjs-mongodb
There is still no clear and scalable solution I see of how to apply Ember to scenario where the Rails backend database like MongoDB stores document of any depth with unknown fields.
There are some ways of normalizing but still with existing REST/REST+DS we may have to write a lot of code to serialize and de serialize.
Take a peek on the discussions:
https://github.com/emberjs/data/issues/53#issuecomment-9196555
https://github.com/emberjs/data/issues/100
If some one finds a better pick, please share with me.
Jus follow the links http://robert-reiz.com/2012/03/05/rails-mongodb-tutorial/, http://www.mongodb.org/display/DOCS/Rails+3+-+Getting+Started

Using Backbone.js along with rails, and JSON file as model

I am new to both rails and backbone, and I need help to figure out how do go about creating an app. I am trying to create a rails app that could read the contents of provided JSON file and perform the required action mention in the JSON file. My application will not have any database but JSON will act as a database (that's my current thought). AS there is no database I am confused about how to use rails model for reading JSON? Should I use rails model or should I use backbone's model to read JSON?
I am not sure of what exactly you are trying to implement.
It seems that you are trying to build a JSON RPC implementation and using an existing rpc backend will save a lot of work for you. The following link might be informative : http://json-rpc.org/
Also I do not understand that how do you plan to implement persistence without a database backend. In case the application does not require persistence the model layer is simply not required. Are you very sure that an MVC framework actually suits your requirements. It does not seem to fit in.

Ruby on Rails Dynamic Models

I'm working on an application where the end user defines what columns a database table should have based off column names in an Excel spreadsheet that gets uploaded or just by manually defining them before uploading the spreadsheet.
Is this something that AR and MySQL could handle or am I better off using mongodb or couchdb?
In the traditional way, I would basically need a new table each time a user uploaded a document. Am I correct in thinking that with mongodb or couchdb, I would just be defining the document instead of a table?
Thanks!
SQL, in general, has a few methods for handling such dynamic column assignments. MongoDB is definitely a much easier paradigm under which to store this data.
The big limitation here will be "what do you do when the data is in the system?". MongoDB has a built-in map-reduce, but this is obviously completely different from set-based SQL.
So to get more help, you'll probably want to detail what happens once the data is in.
You should checkout PostgreSQL hstore. It stores a key-value field that can be indexed. There is support for ActiveRecord here https://github.com/engageis/activerecord-postgres-hstore, which will allow you to just save a Ruby hash on that field.

How to create a user customizable database (like Zoho creator) in Rails?

I'm learning Rails, and the target of my experiments is to realize something similar to Zoho Creator, Flexlist or Mytaskhelper, i.e. an app where the user can create his own database schema and views. What's the best strategy to pursue this?
I saw something about the Entity-Attribute-Value (EAV) but I'm not sure whether it's the best strategy or if there is some support in Rails for it.
If there was any tutorial in Rails about a similar project it would be great.
Probably it's not the easiest star for learning a new language and framework, but it would be something I really plan to do since a long time.
Your best bet will be MongoDB. It is easy to learn (because the query language is JavaScript) and it provides a schema-less data store. I would create a document for each form that defines the structure of the form. Then, whenever a user submits the data, you can put the data into a generic structure and store it in a collection based on the name of the form. In MongoDB collections are like tables, but you can create them on the fly. You can also create indexes on the fly to speed searches.
The problem you are trying to solve is one of the primary use cases for document oriented databases which MongoDB is. There are several other document oriented databases out there, but in my opinion MongoDB has the best API at the moment.
Give the MongoDB Ruby tutorial a read and I am sure you will want to give it a try.
Do NOT use a relational database to do this. Creating tables on the fly will be miserable and is a security hazard, not just for your system, but for the data of your users as well. You can avoid creating tables on the fly by creating a complex schema that tracks the form structures and each field type would require its own table. Rails makes this less painful with polymorphic associations, but it definitely is not pretty.
I think it's not exactly what you want, but this http://github.com/LeonB/has_magic_columns_fork but apparently this does something similar and you may get some idea to get started.
Using a document store like mongodb or couchdb would be the best way forward, as they are schema-less.
It should be possible to generate database tables by sending DDL-statements directly to the server or by dynamical generating a migration. Then you can generate the corresponding ActiveRecord models using Class.new(ActiveRecord::Base) do ... end. In principle this should work, but it has to be done with some care. But this definitely no job for a beginner.
A second solution could be to use MongoMapper and MongoDB. My idea is to use a collection to store the rows of your table and since MongoDB is schema less you can simply add attributes.
Using EntryAttributeValue allows you to store any schema data in a set amount of tables, however the performance implications and maintenance issues this creates may very well not be worth it.
Alternately you could store your data in XML and generate an XML schema to validate against.
All "generic" solutions will have issues with foreign keys or other constraints, uless you do all of that validation in memory before storage.

Rails: Proper work flow to shred XML data into relational SQL database

I have a Rails project and I used Migrations to setup the database schema (I'm using sqlite3). I have a XML file that I want to insert into my database. What's the best way to approach this? I'm thinking there's some Ruby script that I can write once and use to parse the XML file and insert it into my database, but intuitively it feels like this is a common problem and should have already been automated in the Ruby/Rails world. I guess some people would call this XML shredding, but querying Google hasn't turned up much for me.
Any thoughts?
db/seeds.rb could be a good place to put the data.

Resources