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

  • SEARCH
  • Home
  • 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 3791300
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:26:54+00:00 2026-05-19T12:26:54+00:00

I have two different models that can show up in a category. In my

  • 0

I have two different models that can show up in a category. In my routes.rb, I would like to have something like this:

ActionController::Routing::Routes.draw do |map|
  map.top_list ':category/:foo', :controller => 'foo', :action => 'show'
  map.top_list ':category/:bar', :controller => 'bar', :action => 'show'
end

This works fine when I load a URL like “/some-category-name/some-foo-name”, where “some-foo-name” can be loaded by the FooController like so:

class FooController < ApplicationController
  def show
    @foo = Foo.find_by_url! params[:foo]
  end
end

But when I try to request a Bar, like “/some-category-name/some-bar-name”, I get a “ActiveRecord::RecordNotFound in FooController#show”. I know that I can solve this problem by requiring that all Foo names start with “foo-” and all Bar names start with “bar-“, then defining routes like this:

ActionController::Routing::Routes.draw do |map|
  map.top_list ':category/:foo', :controller => 'foo', :action => 'show', :requirements => { :name => /^foo-/ }
  map.top_list ':category/:bar', :controller => 'bar', :action => 'show', :requirements => { :name => /^bar-/ }
end

But forcing this restriction on names is quite suboptimal. I found the following, which looks like it might work for me: Different routes but using the same controller for model subclasses in Rails. However, I don’t quite follow the example, so I don’t know if this would solve my problem. It is also not great that my Foo and BarController would have to inherit from CategoryController.

One thought that occurred to me is that I could try to look up the Foo, then fall back to the BarController if that fails. I can easily do this in the FooController and redirect to the BarController, but this is not really OK, since all the requests for Bars will be logged as FooController#show in the Rails log. However, if I could somehow configure the routes to call a method to determine what to route to based on the basename of the URL, I could get the behaviour that I need; e.g.

ActionController::Routing::Routes.draw do |map|
  is_a_foo = Proc.new {|name| Foo.find_by_url! name && true }

  map.top_list ':category/:foo', :controller => 'foo', :action => 'show', :requirements => { :name => is_a_foo }
  map.top_list ':category/:bar', :controller => 'bar', :action => 'show'
end

Is there any way to do this in bog-standard Rails 2?

  • 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-05-19T12:26:55+00:00Added an answer on May 19, 2026 at 12:26 pm

    I ended up writing a little Rails plugin that allows me to write a route like this:

    map.with_options :category => /[-A-Za-z0-9]+/ do |m|
      # Since both foos and bars can appear under category, we define a route for foo that only matches when
      # the foo can be found in the database, then fall back to the bar route
      m.foo ':category/:foo', :controller => 'foo', :action => 'show', :conditions => {
          :url => { :find_by_url => Foo }
      }
      m.bar ':category/:bar', :controller => 'bar', :action => 'show'
    end
    

    Until I get permission to open source the code, I have to leave the implementation as an exercise for the reader, but here’s how I got there:

    1. Monkey-patching Rails: Extending Routes #2
    2. Under the hood: route recognition in Rails
    3. The Complete Guide to Rails Plugins: Part II

    I’ll update this answer with a github link when I can. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two different tables, lead and click. I would like to query MySQL
I have two different .Net projects, hosted on github. I would like to create
I have a page that displays a table in two different modes. In each
I have two different modules that need access to a single file (One will
I have two models, users and themes, that I'm currently joining in a HABTM
I've got an ASP.NET MVC (VB) project with two models that represent two different
Say that you have an application where different kind of users can sign: Firms,
How can I achieve the following? I have two models (blogs and readers) and
I have two models, Room and Image . Image is a generic model that
Given a table of models 'A', that can have multiple child models 'B', of

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.