<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: ccedacero(Cristian Cedacero)</title>
    <description>The latest articles on DEV Community by ccedacero(Cristian Cedacero) (@ccedacero).</description>
    <link>https://dev.to/ccedacero</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F381105%2F90c324dc-ad2c-4a5d-9260-9a9b81a211b7.png</url>
      <title>DEV Community: ccedacero(Cristian Cedacero)</title>
      <link>https://dev.to/ccedacero</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ccedacero"/>
    <language>en</language>
    <item>
      <title>Better shortcuts with Karabiner Elements and Hammerspoon</title>
      <dc:creator>ccedacero(Cristian Cedacero)</dc:creator>
      <pubDate>Sat, 06 Jun 2020 21:42:24 +0000</pubDate>
      <link>https://dev.to/ccedacero/better-shortcuts-with-karabiner-elements-and-hammerspoon-1plf</link>
      <guid>https://dev.to/ccedacero/better-shortcuts-with-karabiner-elements-and-hammerspoon-1plf</guid>
      <description>&lt;p&gt;Do you ever find yourself wishing you could open your favorite mac os applications with a keyboard shortcut? Tired of pressing 3 to 4 keys to manage windows? Need to easily move a window to another monitor? Well, stop wishing because I’m gonna teach you how you can make this simple.  &lt;/p&gt;

&lt;p&gt;As a keyboard shortcut lover, I’ve always enjoyed sharing my favorite shortcuts, and I’m always on the lookout for new ones. When I learned about the Karabiner Elements and Hammerspoon combo, I knew I needed to set it up, but my initial set up process ended up taking much longer than a simple installation, so I decided to outline the steps here in case this helps anyone.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Karabiner Elements
&lt;/h3&gt;

&lt;p&gt;Karabiner Elements allows you to customize normal key behavior, and it’s complex modifications allow you to map multiple key combinations into one key. We will be taking advantage of this ability to change the behavior of the Caps Lock key.   &lt;/p&gt;

&lt;h5&gt;
  
  
  Hyper Key
&lt;/h5&gt;

&lt;p&gt;We’re going to be mapping the Caps Lock to &lt;strong&gt;command+control+option+shift&lt;/strong&gt;.  This type of key combination is usually referred to as the “hyper key”. We use this four key combination because it is unlikely to interfere with existing shortcuts. Most developerers wouldn’t expect you to press five keys to run a shortcut. We're mapping it to the Caps Lock key because it’s use is limited as you can achieve the same functionality by holding down the shift key. You can also customizer it to another key after trying this out. If you quit Karabiner elements, your Caps Lock key will return to it's normal state. &lt;/p&gt;

&lt;p&gt;We can install Karabiner elements by running:&lt;br&gt;&lt;br&gt;
&lt;code&gt;brew cask install karabiner-elements&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Other installation notes&lt;/p&gt;

&lt;p&gt;You will be prompted to allow apps by the developer, and after installation is complete, you will be prompted for input monitoring permission. This can be allowed in Security and Privacy. Make sure to allow karabiner_grabber and karabiner_observer.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2fyfu07ojdghryhcku16.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2fyfu07ojdghryhcku16.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
For detailed installation instructions, you can see the installation docs &lt;a href="https://karabiner-elements.pqrs.org/docs/manual/configuration/" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Hammerspoon
&lt;/h3&gt;

&lt;p&gt;Hammerspoon is a  desktop automation tool for mac os and it job is to serve as the intermediary between the operating system and a Lua scripting engine, allowing you to interact with multiple mac os APIs. If you just install it, hammerspoon will do nothing,but what makes it so powerful is that you can write your own lua scripts or use one that has been written by the great open source community.The main reason I started using it is because I wanted a simple way to resize windows and move them between multiple monitors. Most shortcut apps require you to press 3 or 4 keys each to resize windows and move between monitors. With the combination of hammerspoon and karabiner, we can do it with two simple keys. You can install hammerspoon with.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;brew cask install Hammerspoon&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The Set Up
&lt;/h3&gt;

&lt;p&gt;Step1. Open Karabiner element and under complex modifications, select the option to import more from the internet. You’ll be redirected to the karabiner complex modifications page, where you can choose from a long list of combinations. Download the Change Caps Lock key(rev4) option.  &lt;/p&gt;

&lt;p&gt;Step2. Once downloaded, enable the Change Caps Lock to command+control+option+shift complex modification.  &lt;/p&gt;

&lt;p&gt;Step3. Verify that it’s working properly by opening Karabiner-Event-Viewer. When you press Caps Lock, you should see your four key combination instead of the Caps Lock key.  &lt;/p&gt;

&lt;p&gt;Step4. Open Hammerspoon and right click on the icon to open the configuration file. This is a lua script where you can write your own scripts or use it to connect to one of the many open source configurations available &lt;a href="https://github.com/Hammerspoon/hammerspoon/wiki/Sample-Configurations" rel="noopener noreferrer"&gt;here&lt;/a&gt;.   &lt;/p&gt;

&lt;p&gt;If you’d like to write your own, you can see the documentation and start with your hello world &lt;a href="http://www.hammerspoon.org/go/#helloworld" rel="noopener noreferrer"&gt;here&lt;/a&gt;.  &lt;/p&gt;

&lt;h6&gt;
  
  
  Window Management
&lt;/h6&gt;

&lt;p&gt;For my window management, I’m using “MiroWindowsManager” which can download from it's repo &lt;a href="https://github.com/miromannino/miro-windows-manager" rel="noopener noreferrer"&gt;here&lt;/a&gt;. Instructions for installing directly from the repo are below. &lt;/p&gt;

&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;Extract the zip file, containing MiroWindowsManager.spoon in ~/.hammerspoon/Spoons&lt;/li&gt;
&lt;li&gt;Now you need to configure Hammerspoon in order to load this spoon in ~/.hammerspoon/Spoons/MiroWindowsManager.spoon adding the following snippet of code in your init.lua file:
&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;local hyper = {"ctrl", "alt", "cmd"}&lt;br&gt;
hs.loadSpoon("MiroWindowsManager")&lt;br&gt;
hs.window.animationDuration = 0.3&lt;br&gt;
spoon.MiroWindowsManager:bindHotkeys({&lt;br&gt;
  up = {hyper, "up"},&lt;br&gt;
  right = {hyper, "right"},&lt;br&gt;
  down = {hyper, "down"},&lt;br&gt;
  left = {hyper, "left"},&lt;br&gt;
  fullscreen = {hyper, "f"}&lt;br&gt;
})&lt;/code&gt;  &lt;/p&gt;

&lt;p&gt;For our set up, you can change the local hyper key variable to our hyper combination:&lt;br&gt;&lt;br&gt;
&lt;code&gt;local hyper = {"cmd", "alt", "ctrl","shift"}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can also adjust the animation duration time up or down as you'd like. Reload your configuration file on hammerspoon and you should now be able to use your Capslock key and an arrow key to move your window to your desired location on your current desktop. Yay! &lt;/p&gt;

&lt;h6&gt;
  
  
  Multiple Monitors
&lt;/h6&gt;

&lt;p&gt;For easy navigating between multiple monitors, you can use the following code written by &lt;a href="https://stackoverflow.com/questions/54151343/how-to-move-an-application-between-monitors-in-hammerspoon" rel="noopener noreferrer"&gt;Karsten S&lt;/a&gt; on stack overflow. &lt;br&gt;
 &lt;code&gt;function moveWindowToDisplay(d)&lt;br&gt;
  return function()&lt;br&gt;
    local displays = hs.screen.allScreens()&lt;br&gt;
    local win = hs.window.focusedWindow()&lt;br&gt;
    win:moveToScreen(displays[d], false, true)&lt;br&gt;
  end&lt;br&gt;
end&lt;br&gt;
hs.hotkey.bind({"ctrl", "alt", "cmd"}, "1", moveWindowToDisplay(1))&lt;br&gt;
hs.hotkey.bind({"ctrl", "alt", "cmd"}, "2", moveWindowToDisplay(2))&lt;br&gt;
hs.hotkey.bind({"ctrl", "alt", "cmd"}, "3", moveWindowToDisplay(3))&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Make sure you replace the key combination with our hyper key combination.You could also place our hyper key definition at the top of the lua file so that you can just call hyper in all other functions instead of writing the key combination. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fxbzp8wd58rb65u0p0z84.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fxbzp8wd58rb65u0p0z84.png" alt="Hyper key definition"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Less repetition&lt;br&gt;&lt;br&gt;
&lt;code&gt;hs.hotkey.bind(hyper, "1", moveWindowToDisplay(1))&lt;br&gt;
&lt;/code&gt;&lt;br&gt;&lt;br&gt;
Now, to switch between multiple monitors, all you have to do is use Caps Lock 1, 2, 3 respectively. I don’t see why this can’t work for more monitors if you edit accordingly. &lt;/p&gt;

&lt;h6&gt;
  
  
  Opening apps
&lt;/h6&gt;

&lt;p&gt;To open any app with your own shortcut, you can add the function below to your lua file. As you can tell, the function checks to see if the app is open and being displayed. If it is, it hides the app. If it's not being displayed, but it's open, it navigates the app. If it's neither open nor hidden, it opens the app. Not sure who the original author is, but thank you whomever you are!&lt;br&gt;&lt;br&gt;
-- open hide app&lt;br&gt;
&lt;code&gt;function openApp(name)&lt;br&gt;
  local app = hs.application.get(name)&lt;br&gt;
  log.i("open App")&lt;br&gt;
  log.i(name)&lt;br&gt;
  log.i(app)&lt;br&gt;
  if app then&lt;br&gt;
    if app:isFrontmost() then&lt;br&gt;
      log.i("app hiding")&lt;br&gt;
      app:hide()&lt;br&gt;
    else&lt;br&gt;
      log.i("app focusing ")&lt;br&gt;
      app:mainWindow():focus()&lt;br&gt;
    end&lt;br&gt;
  else&lt;br&gt;
    log.i("app launch ")&lt;br&gt;
    hs.application.launchOrFocus(name)&lt;br&gt;
  end&lt;br&gt;
end&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After you have this function loaded, you can use it to create your own shortcuts to open any app. Some of personal favorites are below:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;-- VSC&lt;br&gt;
function vscode()&lt;br&gt;
  openApp("Visual Studio Code")&lt;br&gt;
end&lt;br&gt;
hs.hotkey.bind(hyper, 'a', vscode)&lt;/code&gt;&lt;br&gt;
&lt;code&gt;-- Chrome&lt;br&gt;
function chrome()&lt;br&gt;
  openApp("Google Chrome")&lt;br&gt;
end&lt;br&gt;
hs.hotkey.bind(hyper, 'c', chrome)&lt;/code&gt;&lt;br&gt;
&lt;code&gt;-- Evernote&lt;br&gt;
function evernote()&lt;br&gt;
  openApp("Evernote")&lt;br&gt;
end&lt;br&gt;
hs.hotkey.bind(hyper, 'e', evernote)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I know that this a lot of work compared to just being able to download a shortcut app for window management, but having the ability to simplify and define your own keyboard shortcuts is amazing! As developers, we're alway dealing with window management and multiple monitors. And think! This is just the beginning. The possibilities with hammerspoon are endless. If You invest a few days to pick up lua scripting, you’ll be able to write funtions to interact with your system directly! You could be the master of your mac domain. &lt;/p&gt;

&lt;h3&gt;
  
  
  Troubleshooting
&lt;/h3&gt;

&lt;p&gt;If you’re unable to download Karabiner Elements complex modifications, ensure folder permissions are set to write under the karabiner complex modifications folder which is located at &lt;code&gt;~/.config/karabiner/assets/complex_modifications/&lt;/code&gt;. If the folder does not exist, create it, ensure permissions, and try again.&lt;/p&gt;

&lt;p&gt;If you’re able to download and enable complex modifications, but your keystrokes are not registering,and you'd rather not spend time throubleshooting, you can use an alternative workaround by downloading the .json file located below and placing it at &lt;code&gt;~/.config/karabiner/&lt;/code&gt; under the name karabiner.json. &lt;/p&gt;

&lt;p&gt;This will load the hyper key complex modification of &lt;strong&gt;command+control+option+shift&lt;/strong&gt; as default in Karabiner elements. The code is nearly identical to the original config file from karabiner, and you can edit the file to add other combinations from the karabiner website. &lt;/p&gt;

&lt;p&gt;If you add more combinations,take note that this file automatically loads and enables all combinations, so you have to edit accordingly. This file is from Brett Terpstra's 2017 blog post about karabiner elements &lt;a href="https://brettterpstra.com/2017/06/15/a-hyper-key-with-karabiner-elements-full-instructions/" rel="noopener noreferrer"&gt;here&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Thanks for reading and happy coding + shortcuting! &lt;br&gt;
&lt;code&gt;{&lt;br&gt;
    "global": {&lt;br&gt;
        "check_for_updates_on_startup": true,&lt;br&gt;
        "show_in_menu_bar": true,&lt;br&gt;
        "show_profile_name_in_menu_bar": false&lt;br&gt;
    },&lt;br&gt;
    "profiles": [&lt;br&gt;
        {&lt;br&gt;
            "complex_modifications": {&lt;br&gt;
                "rules": [&lt;br&gt;
                    {&lt;br&gt;
                        "manipulators": [&lt;br&gt;
                            {&lt;br&gt;
                                "description": "Change caps_lock to command+control+option+shift.",&lt;br&gt;
                                "from": {&lt;br&gt;
                                    "key_code": "caps_lock",&lt;br&gt;
                                    "modifiers": {&lt;br&gt;
                                        "optional": [&lt;br&gt;
                                            "any"&lt;br&gt;
                                        ]&lt;br&gt;
                                    }&lt;br&gt;
                                },&lt;br&gt;
                                "to": [&lt;br&gt;
                                    {&lt;br&gt;
                                        "key_code": "left_shift",&lt;br&gt;
                                        "modifiers": [&lt;br&gt;
                                            "left_command",&lt;br&gt;
                                            "left_control",&lt;br&gt;
                                            "left_option"&lt;br&gt;
                                        ]&lt;br&gt;
                                    }&lt;br&gt;
                                ],&lt;br&gt;
                                "to_if_alone": [&lt;br&gt;
                                    {&lt;br&gt;
                                        "key_code": "escape"&lt;br&gt;
                                    }&lt;br&gt;
                                ],&lt;br&gt;
                                "type": "basic"&lt;br&gt;
                            }&lt;br&gt;
                        ]&lt;br&gt;
                    }&lt;br&gt;
                ]&lt;br&gt;
            },&lt;br&gt;
            "devices": [&lt;br&gt;
                {&lt;br&gt;
                    "disable_built_in_keyboard_if_exists": false,&lt;br&gt;
                    "identifiers": {&lt;br&gt;
                        "is_keyboard": true,&lt;br&gt;
                        "is_pointing_device": false,&lt;br&gt;
                        "product_id": 610,&lt;br&gt;
                        "vendor_id": 1452&lt;br&gt;
                    },&lt;br&gt;
                    "ignore": false&lt;br&gt;
                },&lt;br&gt;
                {&lt;br&gt;
                    "disable_built_in_keyboard_if_exists": false,&lt;br&gt;
                    "identifiers": {&lt;br&gt;
                        "is_keyboard": true,&lt;br&gt;
                        "is_pointing_device": false,&lt;br&gt;
                        "product_id": 597,&lt;br&gt;
                        "vendor_id": 1452&lt;br&gt;
                    },&lt;br&gt;
                    "ignore": false&lt;br&gt;
                }&lt;br&gt;
            ],&lt;br&gt;
            "fn_function_keys": {&lt;br&gt;
                "f1": "vk_consumer_brightness_down",&lt;br&gt;
                "f10": "mute",&lt;br&gt;
                "f11": "volume_down",&lt;br&gt;
                "f12": "volume_up",&lt;br&gt;
                "f2": "vk_consumer_brightness_up",&lt;br&gt;
                "f3": "vk_mission_control",&lt;br&gt;
                "f4": "vk_launchpad",&lt;br&gt;
                "f5": "vk_consumer_illumination_down",&lt;br&gt;
                "f6": "vk_consumer_illumination_up",&lt;br&gt;
                "f7": "vk_consumer_previous",&lt;br&gt;
                "f8": "vk_consumer_play",&lt;br&gt;
                "f9": "vk_consumer_next"&lt;br&gt;
            },&lt;br&gt;
            "name": "Default profile",&lt;br&gt;
            "one_to_many_mappings": {},&lt;br&gt;
            "selected": true,&lt;br&gt;
            "simple_modifications": {},&lt;br&gt;
            "standalone_keys": {},&lt;br&gt;
            "virtual_hid_keyboard": {&lt;br&gt;
                "caps_lock_delay_milliseconds": 0,&lt;br&gt;
                "keyboard_type": "ansi",&lt;br&gt;
                "standalone_keys_delay_milliseconds": 200&lt;br&gt;
            }&lt;br&gt;
        }&lt;br&gt;
    ]&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Active Record Associations Setup Guide</title>
      <dc:creator>ccedacero(Cristian Cedacero)</dc:creator>
      <pubDate>Thu, 04 Jun 2020 02:19:14 +0000</pubDate>
      <link>https://dev.to/ccedacero/active-record-associations-setup-guide-3aid</link>
      <guid>https://dev.to/ccedacero/active-record-associations-setup-guide-3aid</guid>
      <description>&lt;p&gt;Recently I had to set up a ruby project with Active Record associations and I found myself struggling to piece everything together.I’ve decided to write this blog post to guide you through the setup process and give you a quick overview of model associations. &lt;/p&gt;

&lt;p&gt;The world is full of relationships we can represent using associations. Think, pets and owners, restaurants and customers, patients and doctors, etc. Luckily for us,we can model these relationships with Active Record associations. &lt;/p&gt;

&lt;h1&gt;
  
  
  Most Common Associations
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Has_many
&lt;/h3&gt;

&lt;p&gt;One of the most common rails associations is the has_many association. When you see has_many, think about whether that one thing can have/own many other things. This association represents a one to many association. &lt;/p&gt;

&lt;p&gt;Take a minute to think about the following relationship examples:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An author can have many books
&lt;/li&gt;
&lt;li&gt;A user can have many twitter posts
&lt;/li&gt;
&lt;li&gt;An owner can have many pets
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The has_many relationship exists to help you model these relationships. When in doubt, just  think about whether that specific thing, whatever that may be, can be the owner, have many other things. The naming convention on this association is plural (has_many :appointments).  &lt;/p&gt;

&lt;h3&gt;
  
  
  Has_many_through
&lt;/h3&gt;

&lt;p&gt;When you’re brainstorming your application and user interaction you may realize that one of your models can be related to many others through an intermediary. Think about the following examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A physician has many patients through appointments &lt;/li&gt;
&lt;li&gt;A chef can have many customers through the waiter &lt;/li&gt;
&lt;li&gt;A lyft driver can have many customers through the app &lt;/li&gt;
&lt;li&gt;A pet owner can be related to many vets through it's pet &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In all of these examples,a connection exists through a pass through model (waiter, pet, appointments). This represents a &lt;br&gt;
 has many_through association because we're using a third third model to join them. The naming on this association is plural(has_many :patients, through: :appointments).&lt;/p&gt;

&lt;h3&gt;
  
  
  belongs_to
&lt;/h3&gt;

&lt;p&gt;This association is for models where a one to one connection with another model exists.In other words, one instance of this model must be directly related to it's parent model through a foreign key. This model declaration is singular (belongs_to :Physician). This association It's commonly used in one to many and many to many relationships. Consider the following examples:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Posts belong to users&lt;/li&gt;
&lt;li&gt;Pets belong to owners&lt;/li&gt;
&lt;li&gt;Appointments belong to physicians and patients
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's all on associations for now, If you'd like to dive into rails associations, see the docs &lt;a href="https://guides.rubyonrails.org/association_basics.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up your environment and Migrating
&lt;/h2&gt;

&lt;p&gt;For the remainder of this post, we’ll be implementing a many to many relationship between physicians and patients, seeding them with some data, and looking at some quick examples of how to access data. Let's start by setting up our environment and our migrations.  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fq7wf7zg2igm8ru2hw505.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fq7wf7zg2igm8ru2hw505.png" alt="environment"&gt;&lt;/a&gt;&lt;br&gt;
Our development environment looks like the file tree above. If you wanna follow along, you can download the project folder &lt;a href="https://github.com/ccedacero/Physician_Patient_Model_Example" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We can get started and create our migrations. We can do this with the useful command &lt;code&gt;rake db:create_migration NAME=migration_name&lt;/code&gt;. We can do this to create three empty migration templates inside our migration folder. We'll then define our table schemas. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmnd05d1pdzwmi4h2lqmt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmnd05d1pdzwmi4h2lqmt.png" alt="Patient's table schema"&gt;&lt;/a&gt;&lt;br&gt;
            &lt;/p&gt;
&lt;center&gt;Patient's Table Schema&lt;/center&gt;
&lt;br&gt;
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F4dbkh7m2x8r166fbsxq4.png" alt="physician's table schema"&gt;

&lt;center&gt;Physician's Table Schema&lt;/center&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ft9fvacn86uu1v0k2z635.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ft9fvacn86uu1v0k2z635.png" alt="appointments table schema"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;center&gt;Appointment's Table Schema&lt;/center&gt;

&lt;p&gt;Now that we’ve set up our table schema, let’s migrate with : &lt;code&gt;rake db:migrate&lt;/code&gt;.  &lt;/p&gt;

&lt;p&gt;Once we’re done migrating, our schema file will be populated. Now you can refer to your schema if you need a quick reminder of the table layouts. &lt;/p&gt;

&lt;p&gt;If you need to make a change to your tables, you can rollback with &lt;code&gt;rake db:rollback&lt;/code&gt;, make your changes in the respestive migration files and migrate again. &lt;/p&gt;

&lt;h3&gt;
  
  
  Setting up our Models
&lt;/h3&gt;

&lt;p&gt;At this point, we must head over to our models folder and set up our relationships. Let’s create three files for our models. If you’re using vscode with the code path set, you can just type &lt;code&gt;code physician.rb appointment.rb patient.rb&lt;/code&gt; to get three titled files we can use. &lt;/p&gt;

&lt;p&gt;We will be setting up a has_many_through association. In our association, physicians will have many patients through appointments.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fswvizbct3abhp0hljt2d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fswvizbct3abhp0hljt2d.png" alt="Physician association"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;center&gt;Physician Model &lt;/center&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F56v6c8uqosf7qpbhzfin.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F56v6c8uqosf7qpbhzfin.png" alt="Appointment model"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;center&gt;Appointment Model&lt;/center&gt;

&lt;p&gt;You will notice that our appointments model, reprents the belongs_to association and holds the patient_id and physician_id foreign keys.  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fz9tz1vy5pcltduztdhs5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fz9tz1vy5pcltduztdhs5.png" alt="Patient Association"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;center&gt; Patient Model&lt;/center&gt;

&lt;h5&gt;
  
  
  Why is setting up the association so important?
&lt;/h5&gt;

&lt;p&gt;Because these associations allow us to inherit a lot of commonly used methods from Active Record. Need to delete, update, find, an instance? There’s a method for that. Yay!. &lt;/p&gt;

&lt;h3&gt;
  
  
  Let's try it out
&lt;/h3&gt;

&lt;p&gt;We’ve  come a long way. Let’s make sure our associations are working properly. In order to add some dummy data quickly, I created a seeds.rb file under our db folder and used the faker gem to quickly seed our project with 10 physicians, 10 patients, and 11 appointment instances. This is a small snapshot of what our current tables look like now. &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F6kn2edx3ukxvtxeae6s2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F6kn2edx3ukxvtxeae6s2.png" alt="Appointment Array"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;center&gt;Appointment's array&lt;/center&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fv1gzyyiuhevjjnxpvpz2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fv1gzyyiuhevjjnxpvpz2.png" alt="Physicians Array"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;center&gt;Physician's Array  &lt;/center&gt;

&lt;p&gt;The snapshots above are what you would get by running the name of the model + .all inside a PRY session on your environment. At this point, we have some data, so we can test our relationships. We can select any physician or patient instance and call on appointments to get a list of their appointments and/or corresponding physicians/patients.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3i7l6mmpnidl8bzkzjbn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3i7l6mmpnidl8bzkzjbn.png" alt="A Physician's appointments array"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Running &lt;code&gt;Physician.first.appointments&lt;/code&gt; would select the first physician instance of the physicians array and display their appointments in the appointments tables as seen above.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fl3lte9nn1cpdxj62cvyr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fl3lte9nn1cpdxj62cvyr.png" alt="Patient Name"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the example above, to see the a patient's name, we simply access the desired instance and then access the patient. To see the second appointment's patient instance, we type &lt;code&gt;Physician.first.appointments.last.patient&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fr8ds4s0wx3u6ijfljra8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fr8ds4s0wx3u6ijfljra8.png" alt="Appointment time"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Need the appointment time? use &lt;code&gt;Physician.first.appointments.first.appointment&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I won’t show the patient example, but it would work exactly the same, except that you would substitute physician for patient.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F5xudkxdr1oobaf9kny5y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F5xudkxdr1oobaf9kny5y.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
This works like magic! Yay! We’ve confirmed that our relationships are set up correctly. Our physicians know about their appointment times and their patients, and our patients know about their appointment times and physicians. Did you notice we didn’t build any methods from scratch? That's the Active Record magic! And it’s the reason understanding relationship associations is so important. It just makes life easy and allows you to spend your time coding the hard stuff. &lt;/p&gt;

&lt;p&gt;That is all for now, but if you’d like to deep dive into associations, I recommend you go through the Active Record Associations docs &lt;a href="https://guides.rubyonrails.org/association_basics.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;.  &lt;/p&gt;

&lt;p&gt;If you've made it this far, you deserve a virtual cookie!so feel free to download the project file &lt;a href="https://github.com/ccedacero/Physician_Patient_Model_Example" rel="noopener noreferrer"&gt;here&lt;/a&gt;. I would also give you a real cookie but the technology for sending cookies over the internet hasn't been invented yet, but one day :)  &lt;/p&gt;

&lt;p&gt;To get it set up just navigate to the directory and follow the steps below. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Run &lt;strong&gt;bundle&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;rake console&lt;/strong&gt; to start a pry session and access the model data.&lt;/li&gt;
&lt;li&gt;Try accessing and creating Model data for Appointment, Patient, and Physician. You can do this manually or explore the contents of the seeds.rb file.
&lt;/li&gt;
&lt;li&gt;Have fun! &lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>rails</category>
      <category>activerecord</category>
      <category>rake</category>
      <category>associations</category>
    </item>
  </channel>
</rss>
