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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:35:54+00:00 2026-05-25T10:35:54+00:00

If there’s one thing I’ve learned about Rails 3 is if I’m having a

  • 0

If there’s one thing I’ve learned about Rails 3 is if I’m having a hard time doing something, I’m probably doing it wrong. So I’m looking for help.

I have a few models which are related in a many to many relationship.

I am able to create the associations in the models without a problem. My problem lies in how to build the controllers to work with these relationships. I’ll try and give an example if you don’t see where I’m going with this.

For instance…

class Account < ActiveRecord::Base
    has_many :locations
end

class Contact < ActiveRecord::Base
    has_many :locations
end

class Location < ActiveRecord::Base
    has_and_belongs_to_many :accounts
    has_and_belongs_to_many :contacts
end

Let’s say I have the above models. This would be my resources…

resources :accounts do
    resources :locations
end

resources :contacts do
    resources :locations
end

resources :locations do
    resources :accounts
    resources :contacts
end

So just to keep this shortened a bit, let’s say I want a list of all locations for an account. The above routes would presumably be account/1/locations. Thus landing me at locations#index.

Hopefully I haven’t screwed up my example at this point but what’s the best way to build this action out as it really has multiple jobs… at a minimum the locations for an account, contact, and all locations.

So I end up with something like this…

class LocationController < ApplicationController
    def index
        if params[:account_id]
            @locations = Location.find_all_by_account_id(params[:account_id])
        elsif params[:contact_id]
            @locations = Location.find_all_by_contact_id(params[:account_id])
        else
            @locations = Location.all
        end

        respond_with @locations
    end
end

Update #1: To clarify, as I am getting some answers that suggest I change my Model relationships. I am working with a legacy system in which I can NOT change the relationships at this point. It is ultimately my goal to clean up the database and the relationships but for now I can not. So I need to find a solution that works with this configuration.

  • 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-25T10:35:54+00:00Added an answer on May 25, 2026 at 10:35 am

    Your current approach is not DRY, and would give you a headache if say, for example, you wanted to impose additional scopes on the index; e.g. pagination, ordering, or searching by a field.

    Consider an alternative: Note how your if/elsif/else conditional essentially is just finding the lookup scope to send find to? Why not move that responsibility to a method that does just that? Thus simplifying your actions and removing redundant code.

    def index
      respond_with collection
    end
    
    def show
      respond_with resource
    end
    
    protected
    
    # the collection, note you could apply other scopes here easily and in one place,
    # like pagination, search, order, and so on.
    def collection
      @locations ||= association.all
      #@locations ||= association.where(:foo => 'bar').paginate(:page => params[:page])
    end
    
    # note that show/edit/update would use the same association to find the resource
    # rather than the collection
    def resource
      @location ||= association.find(params[:id])
    end
    
    # if a parent exists grab it's locations association, else simply Location
    def association
      parent ? parent.locations : Location
    end
    
    # Find and cache the parent based on the id in params. (This could stand a refactor)
    #
    # Note the use of find versue find_by_id.  This is to ensure a record_not_found
    # exception in the case of a bogus id passed, which you would handle by rescuing
    # with 404, or whatever.
    def parent
      @parent ||= begin
        if id = params[:account_id]
          Account.find(id)
        elsif id = params[:contact_id]
          Contact.find(id)
        end
      end
    end
    

    inherited_resources is a great gem for cleanly handling scenarios like this. Written by Jose Valim (of Rails). I believe it should work with HABTM, but honestly I’m not positive if I’ve ever tried it.

    The above exmaple is essentially how inherited_resources works, but mostly it works its magic behind the scenes, and you only overwrite methods if you need to. If it works with HABTM (I think it should), you could write your current controller something like this:

    class LocationController < InheritedResources::Base
      belongs_to :contact, :account, :polymorphic => true, :optional => true
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There's a few previous questions on StackOverflow questioning how one goes about accessing local
There are lots of posts on here about moving a folder out of one
There seem to be very different opinions about using transactions for reading from a
There are several shading languages available today like GLSL, HLSL, CG, which one to
There is no doubt that MonoTouch is one of the great cross-compiler(s). Similarly, SenchaTouch
We're building an app, our first using Rails 3, and we're having to build
There are many tutorials that talk about deleting index.php from the url. But I
There is something I miss with the notion of Synchronizing code in Android. Scenario
There is a lot of information about composition vs inheritance online, but I haven't
There is probably an easy answer for this, but when I added DateTime.Now.ToString() to

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.