I'm trying to solve the same problem outlined in this blog post by Tore Darell. Basically, what's the best practice for having multiple access points to a nested resource: IE: class User < ActiveRecord::Base has_many :posts end class Category < ActiveRecord::Base has_many :posts end class Post < ActiveRecord::Base belongs_to :user belongs_to :category end This blog points out one method but I feel like creating a ton of different controllers is the cleanest method, any other thoughts? http://tore.darell.no/entries/2-cleaning-up-your-n...
on 2007-05-19 00:30
on 2007-05-20 19:15
I would say, as long as there's more similarities than differences in the way you retrieve and present posts in the different contexts, stick to doing it DRY, i.e. one controller and one set of views for posts, and use private methods in the controller and helper methods in the view to abstract away the complexity. For example, you could do something like this in the controller: class PostsController < ApplicationController before_filter :find_parent def index @posts = @parent ? @parent.posts.find(:all) : Post.find(:all) end private def find_parent @parent = if params[:category_id] Category.find(params[:category_id]) elsif params[:user_id] User.find(params[:user_id]) end end end