title: Rails: Getting the Parent Object of a Nested Resource
body: <p>I have a nested resource that belongs to many different models. For instance:</p>
  
  <pre><code>resources :users do
    resources :histories, only: [:show]
  end
  
  resources :publications do
    resources :histories, only: [:show]
  end
  
  resources :events do
    resources :histories, only: [:show]
  end
  </code></pre>
  
  <p>In the <code>HistoriesController</code>, I want to find the parent object, though I'm having trouble thinking of a dry way to handle this. At the moment, the best I can come up with is:</p>
  
  <pre><code>if params[:user_id].present?
    @parent = User.find(params[:user_id])
  elsif params[:publication_id].present?
    @parent = Publication.find(params[:publication_id])
  elsif . . . .
  </code></pre>
  
  <p>I've got literally dozens of models I have to branch through in this way, which seems sloppy. Is there a better (perhaps baked-in) approach that I'm not considering?</p>