If I found out about Rails 3, if it's not easy for me to do something, I'm probably wrong. Therefore, I am looking for help.
I have several models that are interconnected in many ways.
I can create associations in models without problems. My problem is how to create controllers to work with these relationships. I will try to give an example if you do not see where I am 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
Say I have the above models. These 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 make it a little shorter, let's say I need a list of all the locations for the account. The above routes would apparently be accounting / 1 / locations. Thus, landing me in the pointers # index.
I hope I have not ruined my example at this point, but the best way to create this action is because it really has several tasks ... at least for the locations for the account, contact, and all locations.
So, I get 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: clarify, as I get some answers that suggest that I am changing my relationship with the model. I work with an outdated system in which I cannot change relationships at the moment. Ultimately, my goal is to clear the database and relationships, but so far I canβt. So I need to find a solution that works with this configuration.