Ruby on Rails


ActiveRecord All Versions

1.0
1.1
1.2
2.0
2.3
3.0
3.1
3.2
4.0
4.1
4.2
5.0

This draft deletes the entire topic.

Introduction

Introduction

expand all collapse all

Examples

  • 6

    Ruby on Rails provides a model generator you can use to create ActiveRecord models. Simply use rails generate model and provide the model name.

    $ rails g model user
    

    In addition to the model file in app/models, the generator will also create:

    • the Test in test/models/user_test.rb
    • the Fixtures in test/fixtures/users.yml
    • the database Migration in db/migrate/XXX_create_users.rb

    You can also generate some fields for the model when generating it.

    $ rails g model user email:string sign_in_count:integer birthday:date
    

    This will create the columns email, sign_in_count and birthday in your database, with the appropriate types.

  • Improvements requested:

    • Other: Scaffolding generator != model generator, the latter doesn't create much and rarely needs cleanup. Typical output is a model file, a migration for its table and a model spec file. –  D-side Jul 22 at 12:19
    3

    Motivation: while using scaffolding is very nice if you are new to Rails or if your application is just starting out, later it can be useful just to do it on your own, avoiding the need to go through the scaffold-generated code to slim it down (remove unused parts, etc.).

    Creating a model can be as simple as creating a file under app/models. You need nothing else to use it from other places.

    The most simple model, in ActiveRecord, is a class that extends ActiveRecord::Base.

    class User < ActiveRecord::Base
    end
    

    Model files are stored in app/models/, and the file name corresponds to the singular name of the class:

    # user
    app/models/user.rb
    
    # SomeModel
    app/models/some_model.rb
    

    The class will inherit all the ActiveRecord features: query methods, validations, callbacks, etc.

    # Searches the User with ID 1
    User.find(1)
    

    Additionally, create your migration to create the backing database table for your new model; see below.

  • 3

    A callback is a method that gets called at specific moments of an object's lifecycle (right before or after creation, deletion, update, validation, saving or loading from the database).

    For instance, say you have a listing that expires within 30 days of creation.

    One way to do that is like this:

    class Listing < ApplicationRecord
      after_create :set_expiry_date
    
      private
    
      def set_expiry_date
        expiry_date = Date.today + 30.days
        self.update_column(:expires_on, expiry_date)
      end
    end
    

    All of the available methods for callbacks are as follows, in the same order that they are called during the operation of each object:

    Creating an Object

    • before_validation
    • after_validation
    • before_save
    • around_save
    • before_create
    • around_create
    • after_create
    • after_save
    • after_commit/after_rollback

    Updating an Object

    • before_validation
    • after_validation
    • before_save
    • around_save
    • before_update
    • around_update
    • after_update
    • after_save
    • after_commit/after_rollback

    Destroying an Object

    • before_destroy
    • around_destroy
    • after_destroy
    • after_commit/after_rollback

    NOTE: after_save runs both on create and update, but always after the more specific callbacks after_create and after_update, no matter the order in which the macro calls were executed.

Please consider making a request to improve this example.

Syntax

Syntax

Parameters

Parameters

Remarks

Remarks

Still have a question about ActiveRecord? Ask Question

Topic Outline