Rails - nesting resource with two parents

2019-04-01 00:40发布

问题:

Say I have a child model with two parent models:

Event has_many tickets

Person has_many tickets

Ticket belongs_to Event
Ticket belongs_to Person

Routes are mapped so Ticket always nests within Event or Person:

resource :people do
  resources :tickets
end

resources :events do
  resources :tickets
end

How do I scope my ticket_Controller CRUD actions by the parent resource?

Right now I'm testing for params and using conditional statements:

class TicketController

  before_filter :get_person
  before_filter :get_event

  def index
    if @person do
      ...
    elsif @event do
      ...
    end
    respond_to
      ...
    end
  end

That seems a bit tedious to do for every action. Is there a more rails-y DRY way to do it?

回答1:

The most DRY would be to use inherited_resources:

class TicketsController < InheritedResources::Base
  belongs_to :event, :person, :polymorphic => true
end

Boom...done. If you can't use inherited_resources for whatever reason, though, rather than get_person or get_event you could set up a filter to get_parent like so:

class TicketsController < ActionController::Base
  before_filter :get_parent

  def get_parent
    if params[:person_id]
      @parent = Person.find(params[:person_id])
      @template_prefix = 'people/tickets/'
    elsif params[:event_id]
      @parent = Event.find(params[:event_id])
      @template_prefix = 'events/tickets/'
    else
      # handle this case however is appropriate to your application...
    end
  end

  # Then you can set up your index to be more generic
  def index
    @tickets = @parent.tickets
    render :template => @template_prefix + 'index'
  end
end

Edit: I added the @template_prefix above to address the template issue you mentioned in your comment.



回答2:

You could do this:

class TicketController

  before_filter :get_object

  def index

  end

  private

  def get_object
    type = params['event_id'] ? 'event' : 'person'
    value = type.classify.constantize.find(params[:"#{type}_id"])
    name = '@' + type
    instance_variable_set(name , value)
  end

end

There are many ways of improving the code above.

You could also write the routes as follows:

resources :people, :has_many => :tickets

resources :events, :has_many => :tickets