What's the difference between generating a scaffold and generating a model in Rails? What are the advantages/disadvantages of doing either?
1. The method to provide students with a scaffold so as to be helped in the building process of their knowledge can be done following different models. The pattern suggested in our study is characterized by adopting a more student-teacher interactive perspective.
Rails scaffolding is a quick way to generate some of the major pieces of an application. If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job.
A Rails Model is a Ruby class that can add database records (think of whole rows in an Excel table), find particular data you're looking for, update that data, or remove data. These common operations are referred to by the acronym CRUD--Create, Remove, Update, Destroy.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With