1

I have an sql file with respective create table statements (and columns, keys etc.). Is there a way to automatically generate models (if not even scaffolds) from this? Thanks in advance

1
  • Rails doesn't have anything out of the box that will do this for you. You could always write a custom generator to parse the sql file and create models. guides.rubyonrails.org/generators.html Commented Dec 29, 2016 at 14:42

2 Answers 2

2

You may want to check out the schema_to_scaffold gem

This Gem generates Rails command strings based on a Rails database schema you already have. Unlike traditional migrations, which modify the database as they generate Rails scaffolding code, this Gem reads the schema for your database and generates the Rails code which matches your database's existing columns.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but I need to create the Rails schema first, I just have an SQL file for now... or is there a way to translate my SQL file into a Rails schema?
@bananaappletw's suggestion solved the main problem, then I applied your solution as next step ;)
1

You could manually use sql file create database.

Simple way is use sqlite, it's easier to setup.

cat db.sql | sqlite3 db/development.sqlite3

Then run rake db:schema:dump to dump database to schema in Rails.

1 Comment

Thanks! I used Postgresql instead. Then I applied @MZaragoza's suggestion ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.