Inherited Resources and optional belongs_to: How to scope collection in parent resource, but not in nested resource?

182 Views Asked by At

I have the following routes:

resources :boilerplates
resources :projects do
  resources :boilerplates
end

The Boilerplate model looks like this:

class Boilerplate < ActiveRecord::Base
  scope :originals, -> { where(prototype_id: nil) }
end

My controller looks like this:

class BoilerplatesController < InheritedResources::Base
  load_and_authorize_resource
  belongs_to :project, optional: true
end

When the URL /boilerplates is opened, I want to display all boilerplates with the originals scope.

When the URL /projects/123/boilerplates is opened, I want the originals scope not to be active.

How can this be achieved?

1

There are 1 best solutions below

0
Joshua Muheim On

I just found a way to do this myself. In BoilerplatesController:

protected

def collection
  if @project
    super
  else
    super.originals
  end
end