Skip to content

Instantly share code, notes, and snippets.

@romchambe
Last active September 18, 2018 13:53
Show Gist options
  • Select an option

  • Save romchambe/f5812797f7cac1513edcda5e1371acb0 to your computer and use it in GitHub Desktop.

Select an option

Save romchambe/f5812797f7cac1513edcda5e1371acb0 to your computer and use it in GitHub Desktop.

Revisions

  1. romchambe revised this gist Jul 16, 2018. 1 changed file with 30 additions and 0 deletions.
    30 changes: 30 additions & 0 deletions rails_models_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -47,3 +47,33 @@ config.generators do |g|
    g.orm :active_record, primary_key_type: :uuid
    end
    ```
    __NB: on uuid use for references and join tables__

    It is important to declare that a given model is using uuids when creating references or join tables, otherwise columns will store the values under wrong types in the DB:
    1. To make a reference with uuids, an example of migration, the `type: :uuid` flag must be added in the association column declaration:
    ```Ruby
    class CreateMessages < ActiveRecord::Migration[5.1]
    def change
    create_table :messages, id: :uuid do |t|
    t.references :player, type: :uuid
    t.text :content
    t.integer :turn_number
    t.boolean :read

    t.timestamps
    end
    end
    end

    ```
    2. To create a join table between models using uuids:
    ```Ruby
    class CreateJoinTableGameInvitesInvitees < ActiveRecord::Migration[5.1]
    def change
    create_join_table :game_invites, :invitees, column_options: {type: :uuid} do |t|
    t.index [:game_invite_id, :invitee_id]
    t.index [:invitee_id, :game_invite_id]
    end
    end
    end
    ```
  2. romchambe revised this gist May 29, 2018. 1 changed file with 15 additions and 1 deletion.
    16 changes: 15 additions & 1 deletion rails_models_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -3,15 +3,29 @@ SSL setup: https://vimeo.com/209534466
    #### Creating a join table
    `rails g migration CreateJoinTableGameInvitesInvitees game_invites invitees`
    - Models linked together must be in alphabetical order when naming the migration file
    - the columns can be given any names as long as they are correctly associated in the model file:
    - The columns can be given any names as long as they are correctly associated in the model file:
    ```Ruby
    has_and_belongs_to_many :invitees, class_name: 'User', foreign_key: 'invitee_id', #the associated column and the alien model
    join_table: 'game_invites_invitees', #name of the join table
    association_foreign_key: 'game_invite_id' #the name of the domestic model in the join table
    ```

    #### Using enums
    It is possible to declare a series of values that will be stored as an integer in the database (so declare the column as integer in the migration file). The series of values must be declared as an array using the enum method in the model. Some methods will be created automatically:
    ```Ruby
    class Conversation < ActiveRecord::Base
    enum status: [ :active, :archived ]
    end

    # Using enum will create some methods
    conversation.active!
    conversation.active? # => true
    conversation.status # => "active"

    # and some scopes
    Conversation.active
    Conversation.archived
    ```

    #### Use UUIDs as database IDs (with pg)
    [Original article](https://lab.io/articles/2017/04/13/uuids-rails-5-1/)
  3. romchambe revised this gist May 29, 2018. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions rails_models_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -5,9 +5,9 @@ SSL setup: https://vimeo.com/209534466
    - Models linked together must be in alphabetical order when naming the migration file
    - the columns can be given any names as long as they are correctly associated in the model file:
    ```Ruby
    has_and_belongs_to_many join_table: 'game_invites_invitees', #name of the join table
    :invitees, class_name: 'User', foreign_key: 'invitee_id', #the alien model
    association_foreign_key: 'game_invite_id #the name of the column of the domestic model
    has_and_belongs_to_many :invitees, class_name: 'User', foreign_key: 'invitee_id', #the associated column and the alien model
    join_table: 'game_invites_invitees', #name of the join table
    association_foreign_key: 'game_invite_id' #the name of the domestic model in the join table
    ```

    #### Using enums
  4. romchambe revised this gist May 29, 2018. 1 changed file with 19 additions and 5 deletions.
    24 changes: 19 additions & 5 deletions rails_models_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,19 @@
    SSL setup: https://vimeo.com/209534466

    ####Creating a join table
    `rails g migration CreateJoinTableProductsSuppliers products suppliers`
    #### Creating a join table
    `rails g migration CreateJoinTableGameInvitesInvitees game_invites invitees`
    - Models linked together must be in alphabetical order when naming the migration file
    - the columns can be given any names as long as they are correctly associated in the model file:
    ```Ruby
    has_and_belongs_to_many join_table: 'game_invites_invitees', #name of the join table
    :invitees, class_name: 'User', foreign_key: 'invitee_id', #the alien model
    association_foreign_key: 'game_invite_id #the name of the column of the domestic model
    ```
    ####Using enums
    #### Using enums
    ####Use UUIDs as database IDs (with pg)
    #### Use UUIDs as database IDs (with pg)
    [Original article](https://lab.io/articles/2017/04/13/uuids-rails-5-1/)
    Supposing we are using Postgre as a database, it only takes the following steps to use UUIDs as database IDs instead of incremental ones:
    @@ -18,4 +25,11 @@ class EnablePgcryptoExtension < ActiveRecord::Migration[5.1]
    enable_extension 'pgcrypto'
    end
    end
    ```
    ```
    3. Tell ActiveRecord that the new generator for IDs should be UUIDs:
    ```Ruby
    #config/application.rb
    config.generators do |g|
    g.orm :active_record, primary_key_type: :uuid
    end
    ```
  5. romchambe revised this gist May 29, 2018. 2 changed files with 21 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions Useful links
    Original file line number Diff line number Diff line change
    @@ -1,2 +0,0 @@
    SSL setup: https://vimeo.com/209534466
    Using uuid in ActiveRecord: https://lab.io/articles/2017/04/13/uuids-rails-5-1/
    21 changes: 21 additions & 0 deletions rails_models_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    SSL setup: https://vimeo.com/209534466

    ####Creating a join table
    `rails g migration CreateJoinTableProductsSuppliers products suppliers`

    ####Using enums


    ####Use UUIDs as database IDs (with pg)
    [Original article](https://lab.io/articles/2017/04/13/uuids-rails-5-1/)

    Supposing we are using Postgre as a database, it only takes the following steps to use UUIDs as database IDs instead of incremental ones:
    1. Generate a migration: `rails g migration EnablePgcryptoExtension`
    2. Modify the migration file to this:
    ```Ruby
    class EnablePgcryptoExtension < ActiveRecord::Migration[5.1]
    def change
    enable_extension 'pgcrypto'
    end
    end
    ```
  6. romchambe revised this gist May 2, 2018. 2 changed files with 2 additions and 1 deletion.
    2 changes: 2 additions & 0 deletions Useful links
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    SSL setup: https://vimeo.com/209534466
    Using uuid in ActiveRecord: https://lab.io/articles/2017/04/13/uuids-rails-5-1/
    1 change: 0 additions & 1 deletion ssl_setup
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    https://vimeo.com/209534466
  7. romchambe created this gist Apr 17, 2018.
    1 change: 1 addition & 0 deletions ssl_setup
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    https://vimeo.com/209534466