Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rails 6: ActiveRecord

Rails 6: ActiveRecord

Nikolay Sverchkov

November 30, 2019
Tweet

More Decks by Nikolay Sverchkov

Other Decks in Programming

Transcript

  1. NIKOLAY SVERCHKOV @ssnickolay
    ACTIVE RECORD 6+

    View Slide

  2. #saintprubyconf @ssnickolay

    View Slide

  3. @ssnickolay
    RAILS 6.0
    MULTIPLE
    DATABASE
    SUPPORT

    View Slide

  4. @ssnickolay
    gem ‘octopus’

    View Slide

  5. @ssnickolay
    gem ‘octopus’
    FOCUSED ON

    View Slide

  6. @ssnickolay
    RAILS 6.0: MULTI DB
    MULTI DB =
    DB SHARDING =
    +
    + +
    VERTICAL OR HORIZONTAL
    PARTITIONING / REPLICATION

    View Slide

  7. @ssnickolay
    RAILS 6.0: MULTI DB
    RAILS DID NOT HAVE
    ADEQUATE* MULTI DB
    SUPPORT
    *https://github.com/ankane/multiverse

    View Slide

  8. @ssnickolay
    RAILS 6.0: MULTI DB
    MULTI DB !!=
    DATABASE SHARDING

    View Slide

  9. @ssnickolay
    AT LEAST 12 HUGE PRS
    FROM @EILEENCODES
    RAILS 6.0: MULTI DB

    View Slide

  10. WHEN YOU
    RELEASE
    THE NEW
    FEATURE

    View Slide

  11. @ssnickolay
    RAILS 6.0: MULTI DB
    MULTI DB !!=
    DATABASE SHARDING
    ?

    View Slide

  12. @ssnickolay
    RAILS 6.0: MULTI DB
    MULTI DB !!=
    DATABASE SHARDING

    View Slide

  13. @ssnickolay
    RAILS 6.0: BULK INSERTS
    # Insert multiple records, performing an upsert
    # when records have duplicate ISBNs
    Book.upsert_all([
    { title: 'Rework', author: 'David', isbn: '1' },
    { title: 'Eloquent Ruby', author: 'Russ', isbn: '1' }
    ],
    unique_by: { columns: %w[ isbn ] })
    *gem ‘activerecord-import`

    View Slide

  14. @ssnickolay
    RAILS 6.0: update
    Rails 4.0.2
    update_attributes(column: value)
    update(column: value)
    Rails 6+
    update(column: value)
    Deprecate `update_attributes`

    View Slide

  15. @ssnickolay
    RAILS 6.0: update
    Rails 4.0.2
    update_attributes(column: value)
    update(column: value)
    Rails 6+
    update(column: value)
    Deprecate `update_attributes`

    View Slide

  16. @ssnickolay
    RAILS 6.0: create_or_find_by
    find_or_create_by
    vs
    create_or_find_by

    View Slide

  17. @ssnickolay
    find_or_create_by
    def find_or_create_by!(attributes, &block)
    find_by(attributes) !||
    create!(attributes, &block)
    end

    View Slide

  18. @ssnickolay
    find_or_create_by
    def find_or_create_by!(attributes, &block)
    find_by(attributes) !|| <Вот-Тут->
    create!(attributes, &block)
    end

    View Slide

  19. @ssnickolay
    RAILS 6.0: create_or_find_by
    def create_or_find_by(attributes, &block)
    transaction(requires_new: true) {
    create(attributes, &block)
    }
    rescue ActiveRecord!::RecordNotUnique
    find_by!(attributes)
    end

    View Slide

  20. @ssnickolay
    RAILS 6.0: create_or_find_by
    def create_or_find_by(attributes, &block)
    transaction(requires_new: true) {
    create(attributes, &block)
    }
    rescue ActiveRecord!::RecordNotUnique
    <Вот-Тут->
    find_by!(attributes)
    end

    View Slide

  21. @ssnickolay
    RAILS 6.0: Other
    • Make t.timestamps with precision by default
    • Add support for UNLOGGED Postgresql tables
    • Add support for annotating queries generated by
    ActiveRecord::Relation with SQL comments
    • Make it possible to override the implicit order column
    • …

    View Slide