title: Fat Model, Skinny Controller in Rails with example
body: <p>I really want to start learning Rails best practices, especially following the "fat model, skinny controller" logic.</p>
  
  <p>Say I have the following comment controller</p>
  
  <pre><code>class CommentsController &lt; ApplicationController
  
      def create
          @post = Post.find(params[:post_id])
          @comment = @post.comments.create(comment_params)
          @comment.user_id = current_user.id if current_user
          @comment.save!
  
          if @comment.save
              redirect_to post_path(@post)
          else
              render 'new'
          end
      end
  
      def edit
          @post = Post.find(params[:post_id])
          @comment = @post.comments.find(params[:id])
      end
  
      def update
          @post = Post.find(params[:post_id])
          @comment = @post.comments.find(params[:id])
  
          if @comment.update(params[:comment].permit(:comment))
              redirect_to post_path(@post)
          else
              render 'Edit'
          end
      end
  
      def destroy
          @post = Post.find(params[:post_id])
          @comment = @post.comments.find(params[:id])
          @comment.destroy
          redirect_to post_path(@post)
      end
  
      private
  
      def comment_params
          params.require(:comment).permit(:comment)
      end
  </code></pre>
  
  <p>What's a good place to start refactoring the code? 
  Immediately I think I an make the <strong>@post</strong> and <strong>@comment</strong> in both <strong>edit</strong> and <strong>update</strong> into a separate method, follow by calling a <strong>before_action</strong> on the method. But that is still putting all the code in the controller.</p>
  
  <p>Are there any code that I can move to the model? If so, how should I structure them?</p>