Koren Leslie Cohen

  • About
  • Blog
  • Contact

31 comments

Ruby / Rails

Rails: Generate Model vs. Resource vs. Scaffold

July 15, 2014 by Koren Leslie Cohen

front end engineer interview questions

If you’re just learning Ruby on Rails, you may be confused as to when to generate individual models, resources or scaffolding, and what files are created by each command.

Say you want to generate a Test model with a name.  You could either generate the model individually, generate resources, or generate scaffolding, as follows:

rails g model Test name:text

rails g resource Test name:text

rails g scaffold Test name:text

What’s the difference between each of the above?

Generating Individual Models

Entering rails g model Test name:text in your command line will generate the following:

(1) A model file test.rb in your models directory:

class Test < ActiveRecord::Base
end

(2) A migration file timestamp_create_tests.rb in your db/migrate directory:

class CreateTests < ActiveRecord::Migration
  def change
    create_table :tests do |t|
      t.text :name

      t.timestamps
    end
  end
end
Generating Resources

Entering rails g resource Test name:text in your command line will generate the following:

(1) A model file test.rb in your models directory:

class Test < ActiveRecord::Base
end

(2) A migration file timestamp_create_tests.rb  in your db/migrate directory:

class CreateTests < ActiveRecord::Migration
  def change
    create_table :tests do |t|
      t.text :name

      t.timestamps
    end
  end
end

(3) a tests_controller.rb file in your controllers directory.  This controller will be an empty shell:

class TestsController < ApplicationController
end

(4) resources :tests routes in your routes.rb file.

Generating Scaffolding

Entering rails g scaffold Test name:text in your command line will generate the following:

(1) A model file test.rb in your models directory:

class Test < ActiveRecord::Base
end

(2) A migration file timestamp_create_tests.rb in your db/migrate directory:

class CreateTests < ActiveRecord::Migration
  def change
    create_table :tests do |t|
      t.text :name

      t.timestamps
    end
  end
end

(3) A tests_controller.rb file in your controllers directory.  When a scaffold is generated, seven public methods and two private methods will be added to your controller:

class TestsController < ApplicationController
  before_action :set_test, only: [:show, :edit, :update, :destroy]

  # GET /tests
  # GET /tests.json
  def index
    @tests = Test.all
  end

  # GET /tests/1
  # GET /tests/1.json
  def show
  end

  # GET /tests/new
  def new
    @test = Test.new
  end

  # GET /tests/1/edit
  def edit
  end

  # POST /tests
  # POST /tests.json
  def create
    @test = Test.new(test_params)

    respond_to do |format|
      if @test.save
        format.html { redirect_to @test, notice: 'Test was successfully created.' }
        format.json { render action: 'show', status: :created, location: @test }
      else
        format.html { render action: 'new' }
        format.json { render json: @test.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /tests/1
  # PATCH/PUT /tests/1.json
  def update
    respond_to do |format|
      if @test.update(test_params)
        format.html { redirect_to @test, notice: 'Test was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @test.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /tests/1
  # DELETE /tests/1.json
  def destroy
    @test.destroy
    respond_to do |format|
      format.html { redirect_to tests_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_test
      @test = Test.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def test_params
      params.require(:test).permit(:name)
    end
end

(4) resources :tests routes in your routes.rb file.

(5) Seven corresponding view files in your views directory: (a) _form.html.erb, (b) edit.html.erb, (c) index.html.erb, (d) index.json.jbuilder, (e) new.html.erb, (f) show.html.erb and (g) show.json.jbuilder. Each view will contain html and embedded ruby.

To learn more about the routes generated by resources, please read Getting Started with Ruby on Rails.

  • About
  • Latest Posts
Connect
Koren Leslie Cohen
Product manager at Facebook. Former senior product manager at Dollar Shave Club in Los Angeles and software engineer at J.Crew / Madewell in New York City. Recovering trial lawyer.
Connect
Latest posts by Koren Leslie Cohen (see all)
  • PM Career Story - April 28, 2022
  • How to Transition into Product Management - December 26, 2017
  • What I’ve Learned in My First Few Months as a Product Manager - October 14, 2015

Related Posts

Rails: Devise Redirects (and how to Customize Devise)
A Beginner's Guide To RSpec

Share

Facebook Google+ Twitter Pinterest Email

Comments Cancel reply

Your email address will not be published. Required fields are marked *

*

code

  1. Brennan Zelener says

    May 13, 2015 at 1:59 am

    Super helpful, thanks Koren!

    Reply
    • Koren Leslie Cohen says

      May 13, 2015 at 7:41 am

      Happy to help! 😉

      Reply
  2. Randall says

    May 17, 2015 at 9:37 am

    I used this today!

    Reply
    • Koren Leslie Cohen says

      May 17, 2015 at 9:55 am

      Awesome, Randall!

      Reply
  3. Josh Frankel says

    June 5, 2015 at 11:07 am

    Excellent post showing the differences between the various rails generators. Clear and concise.

    Reply
    • Koren Leslie Cohen says

      June 5, 2015 at 11:08 am

      Thanks, happy to help!

      Reply
  4. David says

    July 2, 2015 at 8:02 pm

    Very Helpful! Thanks!

    Reply
    • Koren Leslie Cohen says

      July 2, 2015 at 8:21 pm

      No problem! 😉

      Reply
  5. Francisco says

    July 6, 2015 at 12:27 pm

    Very clarifying info. Didn’t know about the resource command.

    Reply
    • Koren Leslie Cohen says

      July 6, 2015 at 12:37 pm

      Happy to help!

      Reply
  6. Danny says

    July 13, 2015 at 2:43 pm

    Very clear and to the point. Excellent explanation, love the examples of the output files. Thanks for this.

    Reply
    • Koren Leslie Cohen says

      July 13, 2015 at 2:45 pm

      Awesome! 🙂

      Reply
  7. Toby says

    July 14, 2015 at 3:38 pm

    Very helpful, and concise! Thank you!

    Reply
    • Koren Leslie Cohen says

      July 14, 2015 at 3:40 pm

      Happy to help!

      Reply
  8. siva says

    January 20, 2016 at 1:36 am

    Really awesome…….thank you so much………..thanks alot…i am fresher ,i started my life with ruby on rails so please suggest me and give some helpfull thing to me….my mail_id: bssp89@gmail.com

    Reply
    • Koren Leslie Cohen says

      July 23, 2016 at 3:08 am

      You’re welcome!

      Reply
  9. Enrika says

    February 29, 2016 at 1:38 am

    Thanks for this!

    Reply
    • Koren Leslie Cohen says

      July 23, 2016 at 3:09 am

      No prob! 😉

      Reply
  10. Raj kumar says

    May 28, 2016 at 1:51 am

    Thanks for such a nice explanation

    Reply
  11. Ben says

    June 26, 2016 at 11:38 pm

    i like this article because it is idiot proof 🙂

    Reply
  12. motaz says

    August 11, 2016 at 9:38 am

    finally , someone did and told us the difference …

    new to rails but i think and everyday i like it more ..

    thank u very much for your kindness .

    Reply
    • Koren Leslie Cohen says

      August 16, 2016 at 7:50 pm

      🙂

      Reply
  13. Ad says

    November 17, 2016 at 11:37 am

    Thanks, I did not know about the resource command option, never used it till today. This help is awesome!

    Reply
  14. Jack says

    December 19, 2016 at 7:03 am

    Cheers for clearing that one up! 😀

    Reply
  15. Sang says

    January 1, 2017 at 11:18 pm

    Wow, I just get into Rails and this is really helpful for me. Thank you so much!

    Reply
  16. Angelo says

    February 27, 2017 at 1:08 am

    I need to know the exact routes. Not the resources :… The get, post thing. Help please.

    Reply
  17. Mike says

    June 13, 2017 at 3:37 am

    Thank You, Koren it was well worth the readand bookmarked

    Reply
    • Koren Leslie Cohen says

      October 20, 2017 at 12:36 pm

      You’re welcome!

      Reply
  18. Rex says

    July 9, 2018 at 12:58 am

    this is the best explanation I’ve seen so far, thank you so much Koren

    Reply

Back to Blog

Trackbacks

  1. Rails: Generate Model vs. Resource vs. Scaffold – dragonken says:
    March 7, 2017 at 4:11 am

    […] Source: Rails: Generate Model vs. Resource vs. Scaffold […]

    Reply
  2. Rails: Generate Model vs. Resourse vs. Scaffold – Chris Kibble says:
    December 20, 2018 at 5:37 pm

    […] Rails: Generate Model vs. Resourse vs. Scaffold […]

    Reply

  • GitHub
  • Instagram
  • LinkedIn
  • RSS
  • Twitter

Looking for something?

Copyright 2023 Koren Leslie Cohen