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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:11:16+00:00 2026-06-18T05:11:16+00:00

Rails: 3.2.11 I have this module in lib and required in application.rb . I’d

  • 0

Rails: 3.2.11

I have this module in lib and required in application.rb. I’d like to make the constant FORBIDDEN_USERNAMES available throughout the app. The constant is an array of values generated from the routes. I can’t make this an initializer because the routes haven’t been loaded yet.

What I have below doesn’t work as FORBIDDEN_USERNAMES returns an empty array.

# in lib
module ForbiddenUsernames    
  def self.names
    Rails.application.reload_routes!
    all_routes = Rails.application.routes.routes

    all_names = Array.new
    all_routes.each do |route|
      # populate all_names array
    end
    all_names.uniq
  end
end

FORBIDDEN_USERNAMES = ForbiddenUsernames.names
# when ForbiddenUsernames.names is called by itself, it does not return [] or nil

Throughout the app, how do I make it so that I can just use FORBIDDEN_USERNAMES? 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-18T05:11:17+00:00Added an answer on June 18, 2026 at 5:11 am

    I don’t understand why you want this to be a constant. It seems to me you could use memoizable behavior.

    # Wherever in your controller. Add helper_method if you need in the view (but would seem wrong)
    def forbidden_usernames
      @forbidden_usernames ||= ForbiddenUsernames.names
    end
    helper_method :forbidden_usernames
    

    ForbiddenUsernames.names will be called if @forbidden_usernames is nil, so it only happen once.

    Update

    # app/models/user.rb
    def forbidden_usernames
      @forbidden_usernames ||= ForbiddenUsernames.names
    end
    
    def validate_not_forbidden
      !forbidden_usernames.include?(self.name)
    end
    

    If ever you need this functionality in more than one model, use modules. You can also have the forbidden_usernames memoized method in the module itself.

    module ForbiddenUsernames    
      def self.names
        @forbidden_names ||= self.populates_all_names
      end
    
      protected
    
      def populate_all_names
        Rails.application.reload_routes!
        all_routes = Rails.application.routes.routes
    
        all_names = Array.new
        all_routes.each do |route|
          # populate all_names array
        end
        all_names.uniq
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a Rails app (which in this case seems like it would
I have this RoR app that calls to RAILS_ROOT. When running it using rails
I am creating a rails app and have used this code in one of
I have had this problem w/ two seperate WYSWYG editors in my rails application
I have a rails helper in my application_helper.rb file that looks like this: def
Rails noob here. I have a rails application with (in this example) three tables.
In my rails 3 app, I have a lib folder: /lib /lib/abc/some_class.rb /lib/abc/some_class/other.rb some_class.rb:
I have a Rails 3 app with a presenter that lives in lib .
I have a module called Resume in the lib directory of my rails 3
The default in Ruby on Rails is to have this set to false (in

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.