The Rails Rollercoaster
Tags: ruby, rails, routes, nesting
Published: 2021-03-20 15:58:58

Rails is so complex it took me a day alone to think about structure. What is a good rollercoaster without a solid foundation? Between my data base, seed data, and models themselves I knew this wouldn't be easy. The challenge: Deciding which routes to take (no pun intended). My initial thought was to nest all routes that had a relationship. For example: 
``` 
schema.rb

create_table "bets", force: :cascade do |t|
    t.boolean "placed"
    t.datetime "time"
    t.integer "odds"
    t.integer "event_id"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

  create_table "betslips", force: :cascade do |t|
    t.boolean "placed"
    t.integer "amount"
    t.string "status"
    t.integer "bet_id"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

  create_table "events", force: :cascade do |t|
    t.string "name"
    t.datetime "time"
    t.integer "league_id"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

  create_table "leagues", force: :cascade do |t|
    t.string "name"
    t.integer "sport_id"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

  create_table "sports", force: :cascade do |t|
    t.string "name"
    t.string "type"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

end
```
``` routes.rb 
    
    resources: sports, only: [:index] do 
      resources: leagues, only: [:index] do
        resources: events, only: [:index, :show] do 
          resources: bets, shallow: true do 
            resources: betslip, shallow: true 
          end
        end
      end
    end
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bstpmth2ctkos9d21ac6.jpeg)As you can see this could get very complicated, very quickly. I thought back to my school lessons and did some googling. The general rule of thumb is routes should never go more than one level deep. This made sense, it was time for a complete make over. I started by connecting to an api endpoint then structured my database, routes and relationships. Using this convention I was able to separate concerns and make restful routing much less complicated. For example:
``` schema.rb

  create_table "bets", force: :cascade do |t|
    t.integer "odd_id"
    t.integer "user_id"
    t.integer "amount"
    t.string "team"
    t.integer "odds"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

  create_table "odds", force: :cascade do |t|
    t.integer "sport_id"
    t.string "sport_key"
    t.string "sport_nice"
    t.string "teams"
    t.string "home_team"
    t.datetime "commence_time"
    t.string "site_and_odds"
    t.string "odds"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

  create_table "sports", force: :cascade do |t|
    t.string "key"
    t.boolean "active"
    t.string "group"
    t.string "details"
    t.string "title"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end
```
``` routes.rb
    
  resources :sports, only: [:index] do
    resources :odds, only: [:index]
  end

  resources :odds, only: [:show] do 
    resources :bets, shallow: true
  end
```
Running `rake routes` before the restructure gave me a huge daisy chain of path helpers that were practically unreadable. The make over enabled me to accomplish my goal and continue to practice restful convention. 

Picture Credit: Pixabay Image by <a href="https://pixabay.com/users/jackkoppa-1144334/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=818322">Jack Koppa</a> from <a href="https://pixabay.com/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=818322">Pixabay</a>
Image by <a href="https://pixabay.com/users/eak_kkk-907811/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=1044889">Eak K.</a> from <a href="https://pixabay.com/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=1044889">Pixabay</a>