Re: Hatred towards Nested Polymorphic Paths

Michael Glaesemann
2007-08-03T22:14:39Z

On Aug 3, 2007, at 17:01 , Brian Hogan wrote:

> Well see that''s exactly why I am confused.
> I understand the data integrity thing and the security thing, but
> how far do you take that?
>
> /users/1/projects/2/tasks/3/notes/4
>
> @user.find(params[:user_id]
> @project  @user.projects.find(params[:project_id]
> @task = @project.tasks.find(params[:task_id]
> @note = @task.notes.find(params[:id]
>
> That just really seems to get messy. Is that really the approach
> you''d take? I assume you''d use some sort of includes
there to
> reduce the db calls then, right?
>

AIUI, In this situation, I''d be in the notes_controller. I''d
have
already set up find_user, find_project, find_task, and
find_note_by_id methods called by before_filter, e.g.,

# in application.rb
private
def find_user
   @user = User.find(params[:user_id])
end

def find_project
   @project = @user.projects.find(params[:project_id])
end

def find_task
   @task = @project.tasks.find(params[:task_id])
end

# in notes_controller.rb
private
before_filter :find_user
before_filter :find_project
before_filter :find_task
before_filter :find_note, :except => [:index, :new, :create]

def find_note_by_id
   @note = @task.notes.find(params[:id])
end

Very succinct, I believe.

Michael Glaesemann
grzm seespotcode net