I am presently designing a database schema for use in a Rails 3.1 application.
At the moment, I am using MySQL Workbench to design the schema visually, and then manually translating this to Rails migrations & models.
Can anyone indicate if there are any solutions that will allow a schema to be designed visually and translated automatically (i.e. via script) to Rails?
Thanks!
First off, the "database-first" approach definitely isn't really the preferred way to work with Rails... but if you really want to...
If you generate the the tables from your schema you can configure the Rails app's config/database.yml file to connect to your database, then call rake db:schema:dump which generates the db/schema.rb file from the database. Then you can create a migration and copy the code from db/schema.rb into the change (or self.up) method.
Note that this does not automatically create model classes - you'll have to create these yourself, remembering to --skip migration in the rails generate model, and possibly needing to make liberal use of the set_table_name (to map the model class to the right table name), alias_attribute (to map model attributes to the right columns), and perhaps set_primary_key.
There were some more complete approaches to this sort of thing for older versions of Rails (Magic Model Generator and reverse_scaffold are two that I've found), but I don't know of any that have been upgraded to work with Rails 3.
SQL Editor is a Mac application that allows you to visually design a DB schema and then export it as something you can easily import as a schema in Rails.
You will still have to create the models yourself.
For rails existing applications or the new application, you can use this for db design
https://dbdiagram.io/
I use railshelper.com which helps me in a simplistic manner to generate quickly some common rails cli commands !
I think Mogwai ERD will help you out, there you can design your ERD and convert it in to a DB schema.
And I think there is no software for design Rails schema, it's just that you make your schema adhere to Rails conventions. But of course Rails can be configured to almost any schema.
Related
The question pretty much says it all.
I'm new to Ruby and Rails and I'm trying to figure out active record. According to a few pieces that I've read, Rails will interrogate the database (in my case PostgreSQL 9.6) and pick up the columns from that. The way I read this, does that mean I can create the database in PostgreSQL, create ORM classes and define the relationships in the ORM classes?
My background is in databases and it's been a while since I've done any web development and prior to 1/8/2018 I had never even touched Ruby let alone Rails. So, the ultimate question is: Can I create the database in PostgreSQL without Rails migrations and then create the ORM classes with the relationships between the classes (tables) defined in them?
Yes, absolutely! You'd just need to connect to the existing db in config/database.yml and generate a schema file by running rake db:schema:dump. Remember to go by Rails naming conventions for ORM Classes (called Models in RoR). You can also explicitly set table names for every model by adding self.table_name = "posts" (see this answer)
I want to create a Rails app but I have a question before start it.
I have defined a database model in paper (about 15 tables) and I don't know which is the best way to start the application:
Create the tables on database with my database client and after that in console do:
rake db:schema:dump
with this I will obtain the shema.rb and after that do a :
rake db:migrate
or
In console, one to one, create the models, edit them with an editor and do:
rake db:migrate
I think the first one is more quickly but the second I think is better from the point of view of rails.
I am bit confused about that, can anyone help me with this question?
Rails mean the only sure way to create a database for a new application - migration.
You can create a single migration for all of your application but such migration is not easy to roll back.
Therefore wise to create a migration for each table.
Normal situation when you have 100,500 migrations in the end. For that you get your application is ready for deploy to combat server.
Permitted the creation of migration through model and scaffold or independently.
Why not use a generator?
rails g model User first_name last_name age:integer email user_name
You don't need to specify type if it's string. This will generate model class and migration.
I think there may be varying viewpoints on this issue.
Also, it depends if you're using the rails built-in sqlite database.
However, assuming that you do use the default rails db configurations,
I personally prefer the second choice.
Later, you might want to change some parts in your tables.
If you follow the rails way of doing so, this process will be very easy and less confusing.
Also, you can always add custom ruby scripts for pre-loading with data.
Of course, other people might find the other way easier.
I'm a PHP programmer for over a decade and making the move to RoR. Here is what I'm used to from the PHP world:
Create DB schema in a tool like MySQL WorkBench -- and make fields precisely the size I want without wasting space (e.g. varchar(15) if it's ip_address).
Write models using Datamapper and place those exact field lengths and specifications in there so my app doesn't try to put in any larger values.
In the RoR world from what I've seen over the past two days, this seems to be the flow suggested:
Add fields / schema using the command line which creates a migration script and apparently created large ass fields (e.g. "ip_address string" is probably making the field varchar(255) in the db when I run the migration).
Put in validations during model creation.
Am I missing something here? What's the process in the RoR world for enterprise level applications where you actually want to create a highly customized schema? Do I manually write out migration scripts?
The scaffolding is what you use to get started quickly. But before running the migration, you can edit it and add constraints and specific column lengths.
Validations specified in the model (in the ruby code) does not carry the same level of security as validations /constraints specified on the database. So you still need to define those on the database.
While it is possible to work with Rails without migrations, I would strongly advice against it. In some cases it cannot be avoided (when working with legacy databases for instance).
The biggest advantage of using the migrations is that your database schema, accross different platforms, can be held in sync through different stages. E.g. your development and your production database. When deploying your code, the migrations will take care that the database is migrated correctly.
You can edit the migration scripts before you run the migration in order to customize the fields.
Yes, if you need to tweak the defaults, you edit the migration scripts.
Also note that you don't need to use migrations, they're a "convenience" while iterating through DB development. There's nothing that says you must use them. The active record pattern doesn't rely on how the DB tables/fields/etc. are created or defined.
For example, migrations are useless when dealing with legacy DBs, but you can still write a Rails app around them.
In Symfony i just have to create models with ORM Designer and export it to symfony as a schema.yml and then use a symfony command to create tables, models and forms.
I wonder if there is an equivalent to the RoR so that you dont have to create models manually by hand?
It saves a lot of time using GUI for this kind of tasks and it is less error-prone.
thanks
it should be possible to support RoR in ORM Designer. The trouble is that we know only PHP and wouldn't be able to beta test RoR support and create unit tests. But if there is anybody willing to help, please let us know at support at orm-designer dot com and RoR support can be released within a few months.
You can use the model generator with script/generate model to create a model class and migration. A great reference on migrations is cheat migrations which is available via the cheat command.
You could use the scaffold generator as per Getting Started with Rails, however scaffolds are "considered harmful" by many (including myself). I prefer to generate an initial migration with script/generate migration, tweak it to suit and then create/update my model, routes, controllers and views manually.
If you are still starting out, you will probably find it helpful to run through Getting Started with Rails at least once.
So what's the best way to create new tables in a Sqlite database in Rails 2. I have created the database using rake db:migrate command. So should I write individual sql scripts to create a database or use rake somehow. I don't need scaffolding.
Basically use migrations.
Some useful help on how to use migrations is available at http://wiki.rubyonrails.org/rails/pages/understandingmigrations and http://wiki.rubyonrails.org/rails/pages/UsingMigrations. A good cheatsheet that I use is also available at http://dizzy.co.uk/ruby_on_rails/cheatsheets/rails-migrations.
Basically migrations use ruby code to create your database tables for you. It is far easier (in my opinion at least) to use nice ruby code to do this rather than SQL DDL - it also does various things automatically for you (like adding id fields to all your tables as rails requires). You can then use rake tasks to actually apply the migrations to your database. The other major advantage that migrations give you is that they are reverseable - so your database is versioned and you can easily jump from one version to another.
Try to avoid writing CREATE/ALTER table scripts and use ActiveRecord migrations instead. A few reasons spring to mind:
Portability: it's much easier to let
AR deal with cross-platform flavour
differences
Change control: your
migrations can manage changes in
both directions with the VERSION=
option, something that's not easy to
do with SQL
It's the Rails way:
follow the Rails conventions unless
you have a compelling reason not to
do so
Simplicity: you don't have to worry about id and timestamp columns when you use migrations, which saves you having to remember them if you work in SQL
If you are not using scaffolding then you should use script/generate migration to create a migration file for each table. There is no need to use sql scripts. After creating some migrations you can apply them to your database using rake db:migrate.