Is it possible to How can I generate a Rails schema from a JSON variable?
I'm looking to go from a JSON variable like this:
{
"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]
}
to having a Model called Employee with 2 fiels: firstName and lastName
You don't generate schemas dynamically in a relational database. Even if its possible to monkey around and parse the JSON and use it to create SQL statements like:
CREATE TABLE 'employees' ...
Its just not an intelligent way to build web applications. It creates a ton of potential security issues and is not sane or maintainable.
It violates the entire way you should be using ActiveRecord - with migrations that build up the schema to a model that works for the problem domain.
If you want to store data in Postgres with an arbitrary schema use a JSON type column.
create_table :employees do |t|
t.json 'data'
end
This will let you store whatever you want in employees.data.