Skip to content

Instantly share code, notes, and snippets.

@john-hamnavoe
Created December 9, 2022 11:13
Show Gist options
  • Save john-hamnavoe/dcf50058a9a29089fe9b4fdf5409ae26 to your computer and use it in GitHub Desktop.
Save john-hamnavoe/dcf50058a9a29089fe9b4fdf5409ae26 to your computer and use it in GitHub Desktop.
Random notes on model generation rails

Introduction

Some notes on syntax on models always looking up.

Foreign Keys

Optional

  • Change Migration to null: true e.g. t.references :vehicle, null: true, foreign_key: true
  • add optional: true to model e.g. belongs_to :vehicle, optional: true

Key Name different that table Name

e.g. key is driver_id table is employee

  • Change Migration so foreign_key is explict to table e.g. foreign_key: { to_table: :employees }
  • Change Model to define class e.g. , class_name: "Employee"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment