Skip to content

Instantly share code, notes, and snippets.

@Atlas7
Last active October 10, 2020 20:23
Show Gist options
  • Save Atlas7/011ba4076844a2bb8a3c to your computer and use it in GitHub Desktop.
Save Atlas7/011ba4076844a2bb8a3c to your computer and use it in GitHub Desktop.
Rails Console - Basics - new, save, and puts

# Models

After issuing:

rails g model Supplier
rails g model Account

Say I have these two models: 'SupplierandAccount`:

supplier.rb

class Supplier < ActiveRecord::Base
  has_one :account
end

account.rb

class Account < ActiveRecord::Base
  belongs_to :supplier
end

Migrations

The corresponding migrations may look like:

20160301092603_create_suppliers.rb

class CreateSuppliers < ActiveRecord::Migration
  def change
    create_table :suppliers do |t|
      t.string :name
      t.timestamps null: false
    end
  end
end

20160301092619_create_accounts.rb

class CreateAccounts < ActiveRecord::Migration
  def change
    create_table :accounts do |t|
      t.integer :supplier_id
      t.string :account_number
      t.timestamps null: false
    end
    add_index :accounts, :supplier_id
  end
end

(then do a rake db:migrate to create tables)

# Console IRB tests

Rails console test run:

irb(main):033:0> Supplier.all
  Supplier Load (0.2ms)  SELECT "suppliers".* FROM "suppliers"
=> #<ActiveRecord::Relation []>

irb(main):034:0> Account.all
  Account Load (0.2ms)  SELECT "accounts".* FROM "accounts"
=> #<ActiveRecord::Relation []>

irb(main):035:0> @supplier = Supplier.new
=> #<Supplier id: nil, name: nil, created_at: nil, updated_at: nil>

irb(main):036:0> @supplier[:name] = "Tesco"
=> "Tesco"

irb(main):037:0> @supplier.save
   (0.1ms)  begin transaction
  SQL (0.4ms)  INSERT INTO "suppliers" ("name", "created_at", "updated_at") VALUES (?, ?, ?)  [["name", "Tesco"], ["created_at", "2016-03-01 10:03:56.010103"], ["updated_at", "2016-03-01 10:03:56.010103"]]
   (2.4ms)  commit transaction
=> true

irb(main):038:0> @suppliers = Supplier.all
  Supplier Load (0.2ms)  SELECT "suppliers".* FROM "suppliers"
=> #<ActiveRecord::Relation [#<Supplier id: 4, name: "Tesco", created_at: "2016-03-01 10:03:56", updated_at: "2016-03-01 10:03:56">]>

irb(main):040:0> @supplier = Supplier.new
=> #<Supplier id: nil, name: nil, created_at: nil, updated_at: nil>

irb(main):041:0> @supplier.name = "Sainsbury"
=> "Sainsbury"

irb(main):042:0> @supplier.save
   (0.1ms)  begin transaction
  SQL (0.5ms)  INSERT INTO "suppliers" ("name", "created_at", "updated_at") VALUES (?, ?, ?)  [["name", "Sainsbury"], ["created_at", "2016-03-01 10:06:56.321467"], ["updated_at", "2016-03-01 10:06:56.321467"]]
   (2.5ms)  commit transaction
=> true

irb(main):049:0> @suppliers.each {|supplier| puts supplier[:name]}
Tesco
Sainsbury
=> [#<Supplier id: 4, name: "Tesco", created_at: "2016-03-01 10:03:56", updated_at: "2016-03-01 10:03:56">, #<Supplier id: 5, name: "Sainsbury", created_at: "2016-03-01 10:06:56", updated_at: "2016-03-01 10:06:56">]

irb(main):050:0>

rb(main):055:0> @account = Account.new(supplier_id: @supplier.id, account_number: "h1234567")
=> #<Account id: nil, supplier_id: 5, account_number: "h1234567", created_at: nil, updated_at: nil>

irb(main):057:0> @account.save
   (0.1ms)  begin transaction
  SQL (0.5ms)  INSERT INTO "accounts" ("supplier_id", "account_number", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["supplier_id", 5], ["account_number", "h1234567"], ["created_at", "2016-03-01 10:33:19.469068"], ["updated_at", "2016-03-01 10:33:19.469068"]]
   (2.3ms)  commit transaction
=> true

irb(main):064:0> @account
=> #<Account id: 1, supplier_id: 5, account_number: "h1234567", created_at: "2016-03-01 10:33:19", updated_at: "2016-03-01 10:33:19">

# reference vs index

Use reference on accounts:

class CreateAccounts < ActiveRecord::Migration
  def change
    create_table :accounts do |t|
      t.string :account_number
      t.timestamps null: false
    end
    add_reference :accounts, :supplier, index:true, foreign_key: true
  end
end

Schema:

ActiveRecord::Schema.define(version: 20160301092619) do

  create_table "accounts", force: :cascade do |t|
    t.string   "account_number"
    t.datetime "created_at",     null: false
    t.datetime "updated_at",     null: false
    t.integer  "supplier_id"
  end

  add_index "accounts", ["supplier_id"], name: "index_accounts_on_supplier_id"

  create_table "suppliers", force: :cascade do |t|
    t.string   "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

end

Use index on accounts:

class CreateAccounts < ActiveRecord::Migration
  def change
    create_table :accounts do |t|
      t.integer :supplier_id
      t.string :account_number
      t.timestamps null: false
    end
    add_index :accounts, :supplier_id
  end
end

Schema:

ActiveRecord::Schema.define(version: 20160301092619) do

  create_table "accounts", force: :cascade do |t|
    t.integer  "supplier_id"
    t.string   "account_number"
    t.datetime "created_at",     null: false
    t.datetime "updated_at",     null: false
  end

  add_index "accounts", ["supplier_id"], name: "index_accounts_on_supplier_id"

  create_table "suppliers", force: :cascade do |t|
    t.string   "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment