Skip to content

Instantly share code, notes, and snippets.

@pablojimeno
Forked from zakariaf/rails7_before_after.md
Created February 22, 2022 08:21
Show Gist options
  • Select an option

  • Save pablojimeno/a85a0d3971d8c960ef79c090809e2395 to your computer and use it in GitHub Desktop.

Select an option

Save pablojimeno/a85a0d3971d8c960ef79c090809e2395 to your computer and use it in GitHub Desktop.

Revisions

  1. @zakariaf zakariaf revised this gist Dec 27, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions rails7_before_after.md
    Original file line number Diff line number Diff line change
    @@ -229,8 +229,8 @@ active_users = User.active
    inactive_users = User.active.invert_where
    ```

    More examples: https://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-invert_where
    Side effects of `invert_where`: https://blog.kiprosh.com/side-effects-of-activerecords-new-feature-invert_where-in-rails-7/
    - More examples: https://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-invert_where
    - Side effects of `invert_where`: https://blog.kiprosh.com/side-effects-of-activerecords-new-feature-invert_where-in-rails-7/

    # Add `associated` method
    It returns the list of all records that have an association
  2. @zakariaf zakariaf revised this gist Dec 26, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rails7_before_after.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    # Rails 7 new features. Before and After
    - [Add ComparisonValidator](add-comparisonvalidator)
    - [Add ComparisonValidator](#add-comparisonvalidator)
    - [PostgreSQL generated columns](#postgresql-generated-columns)
    - [PostgreSQL custom enum types](#postgresql-custom-enum-types)
    - [Add tracking of belongs_to association](#add-tracking-of-belongs_to-association)
  3. @zakariaf zakariaf revised this gist Dec 26, 2021. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions rails7_before_after.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,16 @@
    # Rails 7 new features. Before and After
    - [Add ComparisonValidator](add-comparisonvalidator)
    - [PostgreSQL generated columns](#postgresql-generated-columns)
    - [PostgreSQL custom enum types](#postgresql-custom-enum-types)
    - [Add tracking of belongs_to association](#add-tracking-of-belongs_to-association)
    - [association_previously_changed? method](#association_previously_changed-method)
    - [Add invert_where method](#add-invert_where-method)
    - [Add associated method](#add-associated-method)
    - [Add missing method](#add-missing-method)
    - [Active Record Encryption](#active-record-encryption)
    - [Disable partial_inserts as default](#disable-partial_inserts-as-default)
    - [Active storage pre-defined variants](#active-storage-pre-defined-variants)

    # Add ComparisonValidator

    ### Before Rails 7
  4. @zakariaf zakariaf revised this gist Dec 26, 2021. 1 changed file with 23 additions and 1 deletion.
    24 changes: 23 additions & 1 deletion rails7_before_after.md
    Original file line number Diff line number Diff line change
    @@ -334,4 +334,26 @@ The `INSERT` command includes `description` too, even when we don't pass `descri
    Post Create (1.7ms) INSERT INTO "posts" ("title", "description", "created_at", "updated_at") VALUES (?, ?, ?) [["title", "Rails 7"], ["description", ""], ["created_at", "2021-12-25 20:31:01.420712"], ["updated_at", "2021-12-25 20:31:01.420712"]]
    ```

    More details: https://blog.kiprosh.com/rails-7-introduces-partial-inserts-config-for-activerecord/
    More details: https://blog.kiprosh.com/rails-7-introduces-partial-inserts-config-for-activerecord/

    # Active storage pre-defined variants
    ### Before
    ```ruby
    class Puppy < ApplicationRecord
    has_one_attached :photo
    end
    ```

    `<%= image_tag puppy.photo.variant(resize_to_fill: [250, 250]) %>`

    ### After
    ```ruby
    class Puppy < ApplicationRecord
    has_one_attached :photo do |attachable|
    attachable.variant :thumb, resize: "100x100"
    attachable.variant :medium, resize: "300x300", monochrome: true
    end
    end
    ```

    `<%= image_tag puppy.photo.variant(:thumb) %>`
  5. @zakariaf zakariaf revised this gist Dec 25, 2021. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions rails7_before_after.md
    Original file line number Diff line number Diff line change
    @@ -112,6 +112,9 @@ end
    ```
    Enums will be presented correctly in `schema.rb`, means no need to switch to `structure.sql` anymore :D


    Tutorial for Rails < 7: https://medium.com/@diegocasmo/using-postgres-enum-type-in-rails-799db99117ff

    # Add tracking of `belongs_to` association

    ```ruby
  6. @zakariaf zakariaf revised this gist Dec 25, 2021. 1 changed file with 82 additions and 56 deletions.
    138 changes: 82 additions & 56 deletions rails7_before_after.md
    Original file line number Diff line number Diff line change
    @@ -30,6 +30,88 @@ end
    ```
    more details: https://blog.kiprosh.com/rails7-activerecord-comparison-validator/

    # PostgreSQL generated columns
    ### Before
    One of the options was using callbacks:

    ```ruby
    # == Schema Information
    #
    # Table name: prders
    #
    # id :bigint
    # price :decimal, precision: 8, scale: 2
    # tax :decimal, precision: 8, scale: 2
    # total :decimal, precision: 8, scale: 2
    # created_at :datetime
    # updated_at :datetime

    class Order < ApplicationRecord
    before_save :calculate_total

    private

    def calculate_total
    self[:total] = price + tax
    end
    end

    ```

    Result:
    ```ruby
    order = Order.create!(price: 12, tax: 1)
    order.total => 13
    ```

    ### After
    You just need to use `virtual` and all will be done automatically by postgres
    ```ruby
    create_table :orders, force: true do |t|
    t.decimal :price, precision: 8, scale: 2
    t.decimal :tax, precision: 8, scale: 2
    t.virtual :total, type: :decimal, as: 'price + tax', stored: true
    end
    ```

    Result: You need to reload data to get the calculated value form the DB

    ```ruby
    order = Order.create!(price: 12, tax: 1)
    order.total => nil

    order.reload
    order.total => 13

    ```
    More details: https://tejasbubane.github.io/posts/2021-12-18-rails-7-postgres-generated-columns/


    # PostgreSQL custom enum types
    ### Before
    ```ruby
    def up
    execute <<-SQL
    CREATE TYPE mood_status AS ENUM ('happy', 'sad');
    SQL
    add_column :cats, :current_mood, :mood_status
    end
    ```
    And we had to set `config.active_record.schema_format = :sql` to use `structure.sql` instead of `schema.rb`

    ### After
    In migrations, use `create_enum` to add a new enum type, and `t.enum` to add a column.
    ```ruby
    def up
    create_enum :mood, ["happy", "sad"]

    change_table :cats do |t|
    t.enum :current_mood, enum_type: "mood", default: "happy", null: false
    end
    end
    ```
    Enums will be presented correctly in `schema.rb`, means no need to switch to `structure.sql` anymore :D

    # Add tracking of `belongs_to` association

    ```ruby
    @@ -207,62 +289,6 @@ INSERT INTO `posts` (`title`) VALUES ('{\"p\":\"n7J0/ol+a7DRMeaE\",\"h\":{\"iv\"
    * Guide1: https://edgeguides.rubyonrails.org/active_record_encryption.html
    * Guide2: https://blog.kiprosh.com/activerecord-encryption-in-rails-7/

    # PostgreSQL generated columns
    ### Before
    One of the options was using callbacks:

    ```ruby
    # == Schema Information
    #
    # Table name: prders
    #
    # id :bigint
    # price :decimal, precision: 8, scale: 2
    # tax :decimal, precision: 8, scale: 2
    # total :decimal, precision: 8, scale: 2
    # created_at :datetime
    # updated_at :datetime

    class Order < ApplicationRecord
    before_save :calculate_total

    private

    def calculate_total
    self[:total] = price + tax
    end
    end

    ```

    Result:
    ```ruby
    order = Order.create!(price: 12, tax: 1)
    order.total => 13
    ```

    ### After
    You just need to use `virtual` and all will be done automatically by postgres
    ```ruby
    create_table :orders, force: true do |t|
    t.decimal :price, precision: 8, scale: 2
    t.decimal :tax, precision: 8, scale: 2
    t.virtual :total, type: :decimal, as: 'price + tax', stored: true
    end
    ```

    Result: You need to reload data to get the calculated value form the DB

    ```ruby
    order = Order.create!(price: 12, tax: 1)
    order.total => nil

    order.reload
    order.total => 13

    ```
    More details: https://tejasbubane.github.io/posts/2021-12-18-rails-7-postgres-generated-columns/

    # Disable partial_inserts as default
    ```ruby
    # == Schema Information
  7. @zakariaf zakariaf revised this gist Dec 25, 2021. 1 changed file with 23 additions and 0 deletions.
    23 changes: 23 additions & 0 deletions rails7_before_after.md
    Original file line number Diff line number Diff line change
    @@ -108,6 +108,7 @@ end
    ```

    More details: https://blog.kiprosh.com/rails-7-supports-tracking-of-belongs_to-association/

    # Add `invert_where` method
    Allows you to invert an entire where clause instead of manually applying conditions.

    @@ -133,6 +134,28 @@ inactive_users = User.active.invert_where
    More examples: https://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-invert_where
    Side effects of `invert_where`: https://blog.kiprosh.com/side-effects-of-activerecords-new-feature-invert_where-in-rails-7/

    # Add `associated` method
    It returns the list of all records that have an association

    ### Before
    `User.where.not(contact_id: nil)`

    ### After
    `User.where.associated(:contact)`

    more examples: https://api.rubyonrails.org/classes/ActiveRecord/QueryMethods/WhereChain.html#method-i-associated

    # Add `missing` method
    It returns the list of all records that don't have an association. opposite of `associated`

    ### Before
    `User.where(contact_id: nil)`

    ### After
    `User.where.missing(:contact)`

    more examples: https://api.rubyonrails.org/classes/ActiveRecord/QueryMethods/WhereChain.html#method-i-missing

    # Active Record Encryption
    ### without encryption
    ```ruby
  8. @zakariaf zakariaf revised this gist Dec 25, 2021. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions rails7_before_after.md
    Original file line number Diff line number Diff line change
    @@ -107,6 +107,7 @@ end
    => true
    ```

    More details: https://blog.kiprosh.com/rails-7-supports-tracking-of-belongs_to-association/
    # Add `invert_where` method
    Allows you to invert an entire where clause instead of manually applying conditions.

  9. @zakariaf zakariaf revised this gist Dec 25, 2021. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions rails7_before_after.md
    Original file line number Diff line number Diff line change
    @@ -28,6 +28,7 @@ class Event < ApplicationRecord
    validates_comparison_of :end_date, greater_than: :start_date
    end
    ```
    more details: https://blog.kiprosh.com/rails7-activerecord-comparison-validator/

    # Add tracking of `belongs_to` association

    @@ -236,6 +237,7 @@ order.reload
    order.total => 13

    ```
    More details: https://tejasbubane.github.io/posts/2021-12-18-rails-7-postgres-generated-columns/

    # Disable partial_inserts as default
    ```ruby
    @@ -278,3 +280,5 @@ The `INSERT` command includes `description` too, even when we don't pass `descri

    Post Create (1.7ms) INSERT INTO "posts" ("title", "description", "created_at", "updated_at") VALUES (?, ?, ?) [["title", "Rails 7"], ["description", ""], ["created_at", "2021-12-25 20:31:01.420712"], ["updated_at", "2021-12-25 20:31:01.420712"]]
    ```

    More details: https://blog.kiprosh.com/rails-7-introduces-partial-inserts-config-for-activerecord/
  10. @zakariaf zakariaf revised this gist Dec 25, 2021. 1 changed file with 42 additions and 42 deletions.
    84 changes: 42 additions & 42 deletions rails7_before_after.md
    Original file line number Diff line number Diff line change
    @@ -29,48 +29,6 @@ class Event < ApplicationRecord
    end
    ```

    # Disable partial_inserts as default
    ```ruby
    # == Schema Information
    #
    # Table name: posts
    #
    # id :bigint
    # title :string
    # description :text
    # created_at :datetime
    # updated_at :datetime

    class Post < ApplicationRecord
    end

    ```
    ### Before
    It's **enabled** as default

    `Rails.configuration.active_record.partial_inserts => true`

    The `INSERT` command does not include `description` as we are just passing `title` to the `Post.new` command

    ```ruby
    > Post.new(title: 'Rails 7').save

    Post Create (1.7ms) INSERT INTO "posts" ("title", "created_at", "updated_at") VALUES (?, ?, ?) [["title", "Rails 7"], ["created_at", "2021-12-25 20:31:01.420712"], ["updated_at", "2021-12-25 20:31:01.420712"]]
    ```

    ### After
    It's **disabled** as default

    `Rails.configuration.active_record.partial_inserts => false`

    The `INSERT` command includes `description` too, even when we don't pass `description` to the `Post.new` command

    ```ruby
    > Post.new(title: 'Rails 7').save

    Post Create (1.7ms) INSERT INTO "posts" ("title", "description", "created_at", "updated_at") VALUES (?, ?, ?) [["title", "Rails 7"], ["description", ""], ["created_at", "2021-12-25 20:31:01.420712"], ["updated_at", "2021-12-25 20:31:01.420712"]]
    ```

    # Add tracking of `belongs_to` association

    ```ruby
    @@ -278,3 +236,45 @@ order.reload
    order.total => 13

    ```

    # Disable partial_inserts as default
    ```ruby
    # == Schema Information
    #
    # Table name: posts
    #
    # id :bigint
    # title :string
    # description :text
    # created_at :datetime
    # updated_at :datetime

    class Post < ApplicationRecord
    end

    ```
    ### Before
    It's **enabled** as default

    `Rails.configuration.active_record.partial_inserts => true`

    The `INSERT` command does not include `description` as we are just passing `title` to the `Post.new` command

    ```ruby
    > Post.new(title: 'Rails 7').save

    Post Create (1.7ms) INSERT INTO "posts" ("title", "created_at", "updated_at") VALUES (?, ?, ?) [["title", "Rails 7"], ["created_at", "2021-12-25 20:31:01.420712"], ["updated_at", "2021-12-25 20:31:01.420712"]]
    ```

    ### After
    It's **disabled** as default

    `Rails.configuration.active_record.partial_inserts => false`

    The `INSERT` command includes `description` too, even when we don't pass `description` to the `Post.new` command

    ```ruby
    > Post.new(title: 'Rails 7').save

    Post Create (1.7ms) INSERT INTO "posts" ("title", "description", "created_at", "updated_at") VALUES (?, ?, ?) [["title", "Rails 7"], ["description", ""], ["created_at", "2021-12-25 20:31:01.420712"], ["updated_at", "2021-12-25 20:31:01.420712"]]
    ```
  11. @zakariaf zakariaf revised this gist Dec 25, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rails7_before_after.md
    Original file line number Diff line number Diff line change
    @@ -259,7 +259,7 @@ order.total => 13
    ```

    ### After
    You just need to use `virtual` and will be done automatically by postgres
    You just need to use `virtual` and all will be done automatically by postgres
    ```ruby
    create_table :orders, force: true do |t|
    t.decimal :price, precision: 8, scale: 2
  12. @zakariaf zakariaf revised this gist Dec 25, 2021. 1 changed file with 56 additions and 1 deletion.
    57 changes: 56 additions & 1 deletion rails7_before_after.md
    Original file line number Diff line number Diff line change
    @@ -222,4 +222,59 @@ INSERT INTO `posts` (`title`) VALUES ('{\"p\":\"n7J0/ol+a7DRMeaE\",\"h\":{\"iv\"
    ```

    * Guide1: https://edgeguides.rubyonrails.org/active_record_encryption.html
    * Guide2: https://blog.kiprosh.com/activerecord-encryption-in-rails-7/
    * Guide2: https://blog.kiprosh.com/activerecord-encryption-in-rails-7/

    # PostgreSQL generated columns
    ### Before
    One of the options was using callbacks:

    ```ruby
    # == Schema Information
    #
    # Table name: prders
    #
    # id :bigint
    # price :decimal, precision: 8, scale: 2
    # tax :decimal, precision: 8, scale: 2
    # total :decimal, precision: 8, scale: 2
    # created_at :datetime
    # updated_at :datetime

    class Order < ApplicationRecord
    before_save :calculate_total

    private

    def calculate_total
    self[:total] = price + tax
    end
    end

    ```

    Result:
    ```ruby
    order = Order.create!(price: 12, tax: 1)
    order.total => 13
    ```

    ### After
    You just need to use `virtual` and will be done automatically by postgres
    ```ruby
    create_table :orders, force: true do |t|
    t.decimal :price, precision: 8, scale: 2
    t.decimal :tax, precision: 8, scale: 2
    t.virtual :total, type: :decimal, as: 'price + tax', stored: true
    end
    ```

    Result: You need to reload data to get the calculated value form the DB

    ```ruby
    order = Order.create!(price: 12, tax: 1)
    order.total => nil

    order.reload
    order.total => 13

    ```
  13. @zakariaf zakariaf revised this gist Dec 25, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions rails7_before_after.md
    Original file line number Diff line number Diff line change
    @@ -221,5 +221,5 @@ INSERT INTO `posts` (`title`) VALUES ('{\"p\":\"n7J0/ol+a7DRMeaE\",\"h\":{\"iv\"
    # => <Post:0x00 id: 1, title: "Rails 7"...>
    ```

    Guide1: https://edgeguides.rubyonrails.org/active_record_encryption.html
    Guide2: https://blog.kiprosh.com/activerecord-encryption-in-rails-7/
    * Guide1: https://edgeguides.rubyonrails.org/active_record_encryption.html
    * Guide2: https://blog.kiprosh.com/activerecord-encryption-in-rails-7/
  14. @zakariaf zakariaf revised this gist Dec 25, 2021. 1 changed file with 51 additions and 4 deletions.
    55 changes: 51 additions & 4 deletions rails7_before_after.md
    Original file line number Diff line number Diff line change
    @@ -55,9 +55,7 @@ The `INSERT` command does not include `description` as we are just passing `titl
    ```ruby
    > Post.new(title: 'Rails 7').save

    TRANSACTION (0.1ms) begin transaction
    Post Create (1.7ms) INSERT INTO "posts" ("title", "created_at", "updated_at") VALUES (?, ?, ?) [["title", "Rails 7"], ["created_at", "2021-12-25 20:31:01.420712"], ["updated_at", "2021-12-25 20:31:01.420712"]]
    TRANSACTION (1.9ms) commit transaction
    ```

    ### After
    @@ -70,9 +68,7 @@ The `INSERT` command includes `description` too, even when we don't pass `descri
    ```ruby
    > Post.new(title: 'Rails 7').save

    TRANSACTION (0.1ms) begin transaction
    Post Create (1.7ms) INSERT INTO "posts" ("title", "description", "created_at", "updated_at") VALUES (?, ?, ?) [["title", "Rails 7"], ["description", ""], ["created_at", "2021-12-25 20:31:01.420712"], ["updated_at", "2021-12-25 20:31:01.420712"]]
    TRANSACTION (1.9ms) commit transaction
    ```

    # Add tracking of `belongs_to` association
    @@ -176,3 +172,54 @@ inactive_users = User.active.invert_where

    More examples: https://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-invert_where
    Side effects of `invert_where`: https://blog.kiprosh.com/side-effects-of-activerecords-new-feature-invert_where-in-rails-7/

    # Active Record Encryption
    ### without encryption
    ```ruby
    > Post.create(title: 'Rails 7')

    INSERT INTO "posts" ("title") VALUES (?) [["title", "Rails 7"]]
    ```

    ### Encryption Before
    We had to write a lot of extra codes, and use a gem (e.g. https://github.com/attr-encrypted/attr_encrypted) or play with `ActiveSupport::MessageEncryptor` (tutorial here: https://pawelurbanek.com/rails-secure-encrypt-decrypt)

    ### After
    ```ruby
    class Post < ApplicationRecord
    encrypts :title
    end
    ```

    ```ruby
    > Post.create(title: 'Rails 7')

    INSERT INTO `posts` (`title`) VALUES ('{\"p\":\"n7J0/ol+a7DRMeaE\",\"h\":{\"iv\":\"DXZMDWUKfp3bg/Yu\",\"at\":\"X1/YjMHbHD4talgF9dt61A==\"}}')
    ```

    Querying non-deterministically encrypted data is impossible:

    ```ruby
    > Post.find_by title: 'Rails 7'
    # => nil
    ```

    If you want to directly query an encrypted column attribute, you'd need to use the deterministic approach. For this, simply use the deterministic: true option during declaration.

    ```ruby
    class Post < ApplicationRecord
    encrypts :title, deterministic: true
    end
    ```

    ```ruby

    > Post.create(title: 'Rails 7')
    INSERT INTO `posts` (`title`) VALUES ('{\"p\":\"n7J0/ol+a7DRMeaE\",\"h\":{\"iv\":\"DXZMDWUKfp3bg/Yu\",\"at\":\"X1/YjMHbHD4talgF9dt61A==\"}}')

    > Post.find_by title: 'Rails 7'
    # => <Post:0x00 id: 1, title: "Rails 7"...>
    ```

    Guide1: https://edgeguides.rubyonrails.org/active_record_encryption.html
    Guide2: https://blog.kiprosh.com/activerecord-encryption-in-rails-7/
  15. @zakariaf zakariaf revised this gist Dec 25, 2021. 1 changed file with 26 additions and 1 deletion.
    27 changes: 26 additions & 1 deletion rails7_before_after.md
    Original file line number Diff line number Diff line change
    @@ -150,4 +150,29 @@ end

    > event.organizer_previously_changed?
    => true
    ```
    ```

    # Add `invert_where` method
    Allows you to invert an entire where clause instead of manually applying conditions.

    ```ruby
    class User
    scope :active, -> { where(accepted: true, locked: false) }
    end
    ```

    ### Before

    ```ruby
    active_users = User.active
    inactive_users = User.where.not(id: User.active.ids)
    ```

    ### After
    ```ruby
    active_users = User.active
    inactive_users = User.active.invert_where
    ```

    More examples: https://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-invert_where
    Side effects of `invert_where`: https://blog.kiprosh.com/side-effects-of-activerecords-new-feature-invert_where-in-rails-7/
  16. @zakariaf zakariaf revised this gist Dec 25, 2021. 1 changed file with 77 additions and 0 deletions.
    77 changes: 77 additions & 0 deletions rails7_before_after.md
    Original file line number Diff line number Diff line change
    @@ -74,3 +74,80 @@ TRANSACTION (0.1ms) begin transaction
    Post Create (1.7ms) INSERT INTO "posts" ("title", "description", "created_at", "updated_at") VALUES (?, ?, ?) [["title", "Rails 7"], ["description", ""], ["created_at", "2021-12-25 20:31:01.420712"], ["updated_at", "2021-12-25 20:31:01.420712"]]
    TRANSACTION (1.9ms) commit transaction
    ```

    # Add tracking of `belongs_to` association

    ```ruby
    class Event
    belongs_to :organizer
    end

    class Organizer
    has_many :events
    end
    ```

    ## `association_changed?` method
    > The `association_changed?` method tells if a different associated object has been assigned and the foreign key will be updated in the next save.
    ### Before
    Tracking the target of a `belongs_to` association was able by checking its foreign key.

    ```ruby
    class Event
    belongs_to :organizer
    before_save :track_change

    private

    def track_change
    if organizer_id_changed?
    #track something
    end
    end
    end
    ```

    ### After
    It's doable by using `association_changed?` method

    ```ruby
    class Event
    belongs_to :organizer
    before_save :track_change

    private

    def track_change
    if organizer_changed?
    #track something
    end
    end
    end
    ```

    ## `association_previously_changed?` method
    > The `association_previously_changed?` method tells if the previous save updated the association to reference a different associated object.
    ```ruby
    > event.organizer
    => #<Organizer id: 1, name: "Organization 1">

    > event.organizer = Organizer.second
    => #<Organizer id: 2, name: "Organization 2">

    > event.organizer_changed?
    => true

    > event.organizer_previously_changed?
    => false

    > event.save!
    => true

    > event.organizer_changed?
    => false

    > event.organizer_previously_changed?
    => true
    ```
  17. @zakariaf zakariaf revised this gist Dec 25, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions rails7_before_after.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    ## Add ComparisonValidator
    # Add ComparisonValidator

    ### Before Rails 7
    ```ruby
    @@ -29,7 +29,7 @@ class Event < ApplicationRecord
    end
    ```

    ## Disable partial_inserts as default
    # Disable partial_inserts as default
    ```ruby
    # == Schema Information
    #
  18. @zakariaf zakariaf created this gist Dec 25, 2021.
    76 changes: 76 additions & 0 deletions rails7_before_after.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,76 @@
    ## Add ComparisonValidator

    ### Before Rails 7
    ```ruby
    class Event < ApplicationRecord
    validates :start_date, presence: true
    validates :end_date, presence: true

    validate :end_date_is_after_start_date

    private

    def end_date_is_after_start_date
    if end_date < start_date
    errors.add(:end_date, 'cannot be before the start date')
    end
    end
    end
    ```

    ### After Rails 7

    ```ruby
    class Event < ApplicationRecord
    validates :start_date, presence: true
    validates :end_date, presence: true

    validates_comparison_of :end_date, greater_than: :start_date
    end
    ```

    ## Disable partial_inserts as default
    ```ruby
    # == Schema Information
    #
    # Table name: posts
    #
    # id :bigint
    # title :string
    # description :text
    # created_at :datetime
    # updated_at :datetime

    class Post < ApplicationRecord
    end

    ```
    ### Before
    It's **enabled** as default

    `Rails.configuration.active_record.partial_inserts => true`

    The `INSERT` command does not include `description` as we are just passing `title` to the `Post.new` command

    ```ruby
    > Post.new(title: 'Rails 7').save

    TRANSACTION (0.1ms) begin transaction
    Post Create (1.7ms) INSERT INTO "posts" ("title", "created_at", "updated_at") VALUES (?, ?, ?) [["title", "Rails 7"], ["created_at", "2021-12-25 20:31:01.420712"], ["updated_at", "2021-12-25 20:31:01.420712"]]
    TRANSACTION (1.9ms) commit transaction
    ```

    ### After
    It's **disabled** as default

    `Rails.configuration.active_record.partial_inserts => false`

    The `INSERT` command includes `description` too, even when we don't pass `description` to the `Post.new` command

    ```ruby
    > Post.new(title: 'Rails 7').save

    TRANSACTION (0.1ms) begin transaction
    Post Create (1.7ms) INSERT INTO "posts" ("title", "description", "created_at", "updated_at") VALUES (?, ?, ?) [["title", "Rails 7"], ["description", ""], ["created_at", "2021-12-25 20:31:01.420712"], ["updated_at", "2021-12-25 20:31:01.420712"]]
    TRANSACTION (1.9ms) commit transaction
    ```