What's the difference between generating a scaffold and generating a model in Rails? What are the advantages/disadvantages of doing either?
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.