I have users, groups, and pages. Each record can have pictures.
Here is my architecture:
- Polymorphic model for
Pictures
. Users, groups, and pages arepicturable
. - Nested
pictures
controller for each of users, groups, and pages (e.g.users/5/pictures
)
Is this the most standard set up? Am I forgetting any Rails convention?
routes.rb
example looks like this:
resources :users do
resources :pictures
pictures_controller.rb
looks like this:
class PicturesController < ApplicationController
before_filter :load_picturable
def show
@picture = @picturable.pictures.find(:id)
end
# Get permissible for different objects.
def load_picturable
resource, id = request.path.split('/')[1,2] # /photos/1
# Couples this controller to format of URL, not ideal.
@picturable = resource.singularize.classify.constantize.find(id)
end
end