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

  • Home
  • SEARCH
  • 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 7537437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:51:07+00:00 2026-05-30T06:51:07+00:00

I have class methods like these in a message model: def delete_all_users_messages(user_id, parent_id) message

  • 0

I have class methods like these in a “message” model:

 def delete_all_users_messages(user_id, parent_id)
          message = Message.find_by_parent_id(parent_id)
          message.children.where(:sender_id => user_id ).update_all(:sender_status => 1)
          message.children.where(:recipient_id => user_id ).update_all(:recipient_status => 1 )
          thread = message.message_thread
          thread.update_attribute(:sender_status, 1) if thread.sender_id == user_id 
          thread.update_attribute(:recipient_status, 1) if thread.recipient_id == user_id 
            if thread.sender_status == 1 && thread.recipient_status == 1
            thread.delete
            Message.delete_all(:parent_id => parent_id)
            end
        end

        def delete_all_users_selected_messages(message_ids, user_id, parent_id)
          Message.where(:id => message_ids, :sender_id => user_id).update_all(:sender_status => 1)
          Message.where(:id => message_ids, :recipient_id => user_id).update_all(:recipient_status => 1)
          Message.where('id != ? AND parent_id = ?', parent_id, parent_id).where(:sender_status => 1, :recipient_status => 1).delete_all
          thread = MessageThread.find_by_message_id(parent_id)
          children_exist = thread.message.children.where('id != ? AND parent_id = ?', parent_id, parent_id).any?
            unless children_exist
              thread.delete 
              thread.message.delete
            end

I use them in my messages_controller like this:

def destroy_all_messages
    Message.delete_all_users_messages(current_user.id, params[:format])
    flash[:success] = "Messages deleted"
    redirect_to messages_path
  end


  def destroy_selected_messages
    Message.delete_all_users_selected_messages(params[:message_ids], current_user.id, params[:format])
      flash[:success] = "Messages deleted"
      redirect_to :back
  end

I would like to also return an error message if something went wrong.. but because all the logic for deleting messages is in my model it’s quite hard to do this.

Can anyone provide a solution with an example of how I could use some kind of condition to show my success message if messages were deleted and an error if they weren’t because something went wrong. There must me some kind of markers I can put in the class methods to confirm they have passed a certain point successfully.

Thanks for your time
Kind regards

  • 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-05-30T06:51:09+00:00Added an answer on May 30, 2026 at 6:51 am

    There are two suggestions I’d make for you here.

    The first is have your methods return false if what they were called to do didn’t happen. So, have something like this:

    if thread.delete && Message.delete_all(:parent_id => parent_id)
      return true
    else
      return false
    end
    

    Then in your controller you can check the status of the method:

    def destroy_all_messages
      if Message.delete_all_users_messages(current_user.id, params[:format])
        flash[:success] = "Messages deleted"
        redirect_to messages_path
      else
        flash[:error] = "Messages could not be deleted"
        render :action => :show
      end
    end
    

    This is the method I would recommend; if you need multiple error messages for different steps in the process, I would break apart your delete_all_user_messages method into separate components, each one indicating true or false for whether they succeeded or failed.

    Alternatively, you could raise custom error messages and rescuing them in your code. Something like this:

    raise ThreadNotDeleted unless thread.delete 
    

    And then in your controller you rescue ThreadNotDeleted and deal with it there. While this works I think the other method is preferable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that contains two methods like these: public String getFoo(Int32 a)
I have a model like this: class Transaction < ActiveRecord::Base def amount self[:amount].abs end
Let's say we have a memory-intensive class like an Image , with chainable methods
I have certain PHP class methods that access external variables. These variables are not
I have the following models: class Post(models.Model): message = models.TextField() (etc.) class UserProfile(models.Model): user
I have a class like so: public class ClassA { public bool MethodA() {
I have a class MyClass, and I would like to override the method ToString()
Suppose that I have a Java class with a static method, like so: class
I have a class with only class methods (utility stuff), so my interface is
I have a class with two methods defined in it. public class Routines {

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.