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 8939027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:45:00+00:00 2026-06-15T10:45:00+00:00

I want to optimize my code How can i ? If I added new

  • 0

I want to optimize my code How can i ?

If I added new role then one more condition add but that i don’t want. I want any no. of roles but i need only one conditions is it possible?

after_save :announcement_send

def announcement_send
  if self.send_now == true && self.group_id.to_s == "Artists"
    User.having_role("artist").each do |user|
      ArtistMailer.announcement_user(self, user).deliver
    end
  elsif self.send_now == true && self.group_id.to_s == "Fans"
    User.having_role("fan").each do |user|
      ArtistMailer.announcement_user(self, user).deliver
    end
  elsif self.send_now == true && self.group_id.to_s == "Both"
     User.not_having_role("admin").each do |user|
       ArtistMailer.announcement_user(self, user).deliver
     end
  end        
end
  • 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-15T10:45:01+00:00Added an answer on June 15, 2026 at 10:45 am

    Not really tested this out but I would break this down like this:

    def role(s)                                                                                                                                                                               
      if s == "Both"                                                                                                                                                                          
        User.not_having_role("admin")                                                                                                                                                         
      else                                                                                                                                                                                    
        User.having_role(s.singularize.downcase)                                                                                                                                              
      end                                                                                                                                                                                     
    end         
    
    if self.send_now                                                                                                                                                                          
      role(self.group_id.to_s).each do |user|                                                                                                                                                 
        ArtistMailer.announcement_user(self, user).deliver                                                                                                                                    
      end                                                                                                                                                                                     
    end 
    

    This code assumes the group_id always comes along as the plural form of the role the users are in. So Artists means the “artist” role.

    Of course this assumes all roles are valid group_ids. If that’s not the case you can check the group_id against a whitelist of possible values inside def role:

    white_list = ['artist', 'fan']
    role = s.singularize.downcase
    if white_list.include? role
      User.having_role(role)
    else
      # possibly throw an exception
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello i want to ask how can i optimize that code: $('#infoTrigger0').live('click', function() {
I got this code, I would like to optimize. I basically can add new
I'm just looking to optimize the code below. It works, but I want to
I have certain code that I want to optimize. It looks like this: function
I have read that some of the JVMs out there can optimize code execution
The code i want to optimize is basically a simple but large arithmetic formula,
I want to optimize my source code Twig but this does not look very
So the situation is: I want to optimize my code some for doing counting
I have the following code: // Dictionary which I want to optimize Dictionary<string, MyClass>
I have some code written in CoffeeScript and I want to optimize the generated

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.