Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8671509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:58:05+00:00 2026-06-12T18:58:05+00:00

The action bellow creates a new comment. A user has many statuses A status

  • 0

The action bellow creates a new comment.

  • A user has many statuses
  • A status has many comments

How can optimize this action so that head 401 and return is not repeated many times.

def create
  @user = User.where(id: params[:user_id]).first
  if @user
    if current_user.friend_with?(@user) or current_user == @user
      @status = @user.statuses.where(id: params[:status_id]).first
      if @status
        @comment = @status.comments.build(params[:comment])
        @comment.owner = current_user
        if @comment.valid?
          @comment.save
          current_user.create_activity(:comment_status, @comment, @user)
        else
          head 401 and return
        end
      else
        head 401 and return
      end
    else
      head 401 and return
    end
  else
    head 401 and return
  end
end

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:58:07+00:00Added an answer on June 12, 2026 at 6:58 pm

    When do you want to return 401?

    1. when a user has not been found
    2. when a user is not a current user or is not a friend of that user
    3. when a status has not been found
    4. when new comment has not been successfully created

    Instead of using so many conditionals, you can use methods that raise exceptions. When you do so, you can rescue from that exceptions with the desired behavior (rendering 401).

    So my suggestions for listed conditions are:

    1. use find! instead of where and then first.
    2. raise something, preferably custom exception (NotAFriendError)
    3. same as 1., use find!
    4. use create!, it’s an equivalent to new and then save! which will raise ActiveRecord::RecordInvalid exception if it fails on validation.

    Here’s the result:

    def create
      begin
        @user = User.find!(params[:user_id])
        raise unless current_user.friend_with?(@user) || current_user == @user
        @status = @user.statuses.find!(params[:status_id])
        @comment = @status.comments.
          create!(params[:comment].merge(:owner => current_user))
      rescue ActiveRecord::RecordNotFound, ActiveRecord::RecordInvalid
        head 401
      end
      # everything went well, no exceptions were raised
      current_user.create_activity(:comment_status, @comment, @user)
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a status update, and comment db table. A user has many status
I have a Rails controller set up that when a user creates a new
Can anyone tell help me understand the new CRUD scaffolding that is included with
I'll preface this with the fact that I'm new to RoR. I have a
I have two functions: one that creates a new <textarea> when a button is
I have been writing some code that creates a generic blog. Its features are
I have the following code which creates a dynamic object that is assigned to
I'm new to iOS programming and I am working on a easy project that
I created a new window based application, and a RootViewController that sublcasses UIViewController. I
The context is this: The user click on a button The page is refreshed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.