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 4010382
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:59:33+00:00 2026-05-20T08:59:33+00:00

Let’s say my app has two models, Foo and Bar. Foo optionally belongs_to Bar.

  • 0

Let’s say my app has two models, Foo and Bar.

Foo optionally belongs_to Bar.

Right now I can look at a single Foo, or search for a particular Foo, and the FoosController handles all that. My URLS are like:
foos/1 and foos/new

Sometimes I want to look at a Bar. The BarsController handles that, and I get to it like:
bars/1 or bars/1/edit.

If I’m looking at a Bar I might want to browse all the Foos that are part of that Bar. So, I’d like to use bars/1/foos/ to look at those Foos.

This is pretty straightforward with nested resources, and it looks like this:

resources :foo
resources :bar do
  resources :foo
end

However, Foos that are part of a Bar are kind of special, set apart from regular Foos. So, for instance, if I load foos/1 or bars/1/foos/1, I would be looking at the same Foo, but I am focused on different information in each case.

So I’ve been thinking about having a BarFoos Controller to handle Foos when they’re in the context of a Bar. However, if I nest BarFoos under Bar, then my helpers are going to be like bar_bar_foos_path and new_bar_bar_foo_path. That seems redundant.

So, now I’m thinking about namespaces, which is something I’ve never looked into before. I see in the rails guide that I could define:

namespace "bar" do
  resources :foos
end

If I do that I can make a second FoosController under app/bar/, and that FoosController can handle Foos inside of a Bar with nice helpers like bar_foo_path(:id) instead of bar_bar_foo_path(:id).

But if I do that, what happens to my BarsController? How do requests get routed to BarsController if instead of resources :bars I have namespace "bar"?

And, lastly, is there anything special I need to do inside my secondary FoosController to make sure there’s not a name conflict with the top-level FoosController? I realize the routing says “namespace”, but how does the rest of the ruby code know that the app/bar/foos_controller and app/foos_controller are not the same class?

Thanks!

  • 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-20T08:59:34+00:00Added an answer on May 20, 2026 at 8:59 am

    I think what you’re trying to achieve is:

    1. Bar has many Foos
    2. View Foos belonging to Bar
    3. View all Foos regardless of parent.

    You can achieve that with:
    routes.rb:

    resources :foos
    resources :bars do
      resources :foos, :controller => 'bars/foos'
    end
    

    The route helpers you end up with are:

    • bars_path
    • foos_path
    • bars_foos_path
    • etc, etc, ‘rake routes’ for the rest
      =)

    In essence, you end up with:

    • app/BarsController (rails g controller bars)
    • app/FoosController (rails g controller foos)
    • app/bars/FoosController (rails g controller bars/foos)

    In FoosController, you would access foos as usual with:

    @foos = Foos.all
    

    and in bars/FoosController, you would access bar’s foos with:

    @foos = @bar.foos
    

    where bar can be pre-retrieved in the bars/foos controller with:

    before_filter :get_client
    
    private
    def get_client
      @bar = Bar.find(params[:bar_id])
    end
    

    Hope this helps. =)

    Edit:
    As for namespaced routes, I’ve personally used them when I some of my resources retrieved from a sub-path. For example, if I have an admin section of my site, then I might have the following:

    routes.rb:

    namespace :admin do
      resources :foos
    end
    

    and I create my controller with:

    rails g controller admin/foos
    

    This sets up my foos resource, such that I can access it at “my site url”/admin/foos, and also get helpers such as admin_foos_path.

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

Sidebar

Related Questions

Let's say I have two entities: Physician Credentials And a physician can have many
Let's say I have two files.. I want to compare them side-by-side and see
Let's say you create a wizard in an HTML form. One button goes back,
Let's say I'm building a data access layer for an application. Typically I have
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I have a drive such as C:\ , and I want to
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say that I have an arbitrary string like `A man + a plan

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.