DEV Community

Cover image for The Rails Rollercoaster
John Glennan
John Glennan

Posted on

The Rails Rollercoaster

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
Enter fullscreen mode Exit fullscreen mode

    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
Enter fullscreen mode Exit fullscreen mode

Alt TextAs 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:


  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
Enter fullscreen mode Exit fullscreen mode

  resources :sports, only: [:index] do
    resources :odds, only: [:index]
  end

  resources :odds, only: [:show] do 
    resources :bets, shallow: true
  end
Enter fullscreen mode Exit fullscreen mode

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 Jack Koppa from Pixabay
Image by Eak K. from Pixabay

Top comments (0)