Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8318773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:05:38+00:00 2026-06-08T22:05:38+00:00

I’m looking for some feedback on both the philosophy and technical practice of using

  • 0

I’m looking for some feedback on both the philosophy and technical practice of using a top level route that is not a catch-all in Rails.

Example:

# routes.rb
get '/cities' => 'cities#index'
get '/:city_id' => 'cities#show', as: :city, city_id:/([a-z\-\d]+)/

I have about 30k cities that :city_id should be limited to matching things like “/los-angeles” but not “/somewrongthing.jpg”. Since this is the last route, it has become a bit of a catch-all. I am currently rendering a 404 if a city is not found, but this means a hit to the DB each time a missing link comes through. I limited it through constraints, but its not quite the elegant solution I was hoping for.

Are there any solutions besides putting it below something like /city/:city_id?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:05:40+00:00Added an answer on June 8, 2026 at 10:05 pm

    I think you should be looking at some alternative to handling this in the routes themselves. 30,000 routes would just trade RAM issues for DB issues.
    Maybe you have some plain ruby object (file backed, maybe?) that can act as a cities cache and you query it on every request that falls through to your city route.

    Something like this, maybe?

    # in config/routes.rb
    get '/:city' => 'cities#show', constraints: CityConstraint.new
    
    # in lib/city_constraint.rb
    class CityConstraint
      def matches?(request)
        city_valid?(request.params[:city])
      end
    
      private
    
      def city_valid?(city)
        # I dunno.. Redis, look it up in a text file, hash, etc.
        # ..look it up in activerecord and cache it at startup?
        # or whatever...
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.