What's the difference between generating a scaffold and generating a model in Rails? What are the advantages/disadvantages of doing either?
4 Answers
When you generate a model, you get a model as well as some related components. One of my favorite ways of explaining topics like this is to actually try it out or encourage others to try it, so if I were to enter the command rails generate model Foo name:string description:text
within a Rails project, I would get:
invoke active_record
create db/migrate/20130719012107_create_foos.rb
create app/models/foo.rb
invoke test_unit
create test/unit/foo_test.rb
create test/fixtures/foos.yml
The first line invokes Active Record
, which basically ties your model to your database. The next line creates what's called a migration file. Migration files contain instructions for altering your database. This first migration file creates the database table called 'foos' and it will also create columns for "name" and "description".
The next line makes the model itself. The model is basically a Ruby class that inherits from Active Record. What this means is that any methods that can be called in Active Record can now be called in your model. The last three lines basically create related test files for your model. If you were using RSpec, spec files would be created instead.
If your Rails application only contained models, you would not have any kind of view that displays information on a page, nor would you have instructions that control the flow of information. Your choices would be to also generate controllers (which in turn generates your views) or to generate a scaffold, which generates your model, views, controller, and writes to your routes.rb file. If I ran rails generate scaffold foo
I would get:
invoke active_record
create db/migrate/20130719013307_create_foos.rb
create app/models/foo.rb
invoke test_unit
create test/unit/foo_test.rb
create test/fixtures/foos.yml
invoke resource_route
route resources :foos
invoke scaffold_controller
create app/controllers/foos_controller.rb
invoke erb
create app/views/foos
create app/views/foos/index.html.erb
create app/views/foos/edit.html.erb
create app/views/foos/show.html.erb
create app/views/foos/new.html.erb
create app/views/foos/_form.html.erb
invoke test_unit
create test/functional/foos_controller_test.rb
invoke helper
create app/helpers/foos_helper.rb
invoke test_unit
create test/unit/helpers/foos_helper_test.rb
invoke assets
invoke coffee
create app/assets/javascripts/foos.js.coffee
invoke scss
create app/assets/stylesheets/foos.css.scss
invoke scss
identical app/assets/stylesheets/scaffolds.css.scss
To answer your question, the advantage of the scaffold is that it's quick, easy, and everything is preconfigured for you. However, the advantages of generating models independently of scaffolds (and then in turn generating controllers/views where needed and writing your routes.rb file yourself) is that you have a lot more control over your app and how it looks and functions, you avoid unnecessary code, you can employ Behaviour-Driven Development or Test Driven Development, and probably other things that someone else might want to add.
My last bit of advice is: Rails is very user-friendly, so try experimenting yourself. You can undo any generate
command with a corresponding destroy
command, so for instance rails destroy scaffold Foo
would delete all the files generated by rails generate Scaffold Foo name:string description:string
, so you don't have to worry about irrevocably messing up a project by experimenting.

- 3,837
- 2
- 32
- 42
-
1I should also encourage you to read any of the official Rails Guides on these topics...I provided a pretty high-level overview of things like Active Record and Migrations, but hopefully enough of one to answer your question! – aceofbassgreg Jul 19 '13 at 01:44
-
I had the same question. Great explanation. Thanks. – whitehat Jan 10 '16 at 23:35
-
3Didn't know about the undo! Thanks for letting me know! +1 – Mark Nov 06 '17 at 14:29
Generating a scaffold includes generating a model but also generates
- RESTful routes
- a controller with all actions for RESTful handling of the model
- views needed by the controller
- tests stubs
Scaffolding is a good start for new Rails users.

- 8,430
- 2
- 35
- 53
With
$rails generate scaffold <name>
you can auto-generate a ready to use controller, model, and views with a full CRUD (Create, Read, Update, Delete) web interface. It is way easier and way faster than generating models, but the code it generates is unlikely to be a perfect for your application.
A scaffold is excellent to use when it comes to simple examples, quick mockups or testing.
If you change your mind and decide to use scaffold after already generating the model, you can always run
$rails generate scaffold <name>
It will create all the missing files. Or if you are ot happy with using scaffold, you can always remove it in the following way:
Generate scaffold:
$rails generate scaffold Story
If you migrated your files, perform a rollback:
$rake db:rollback
Destroy or undo scaffold: $rails destroy scaffold Story

- 6,218
- 42
- 34
New users would most likely be confused by scaffolding as they would not understand how all the components work together. Scaffolding is probably best for experienced users who want a shortcut for work they would typically be repeating.

- 149
- 9