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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:20:50+00:00 2026-06-11T00:20:50+00:00

I have an ActiveRecord based application that is used via command line utilities. Having

  • 0

I have an ActiveRecord based application that is used via command line utilities. Having the models namespaced in such an application is advantageous for keeping the Object namespace clean.

I’m starting to build a rails application around these ActiveRecord models and, though I have overcome some of my initial troubles with using models in a namespace, I’m finding things are more verbose than I’d like.

What I want is to programmaticaly set a namespace for my ActiveRecord classes when used in the command line utilities and to programmaticaly not set a namespace for these models when used in the Rails app.

I know that the files themselves could be altered at runtime before being required, but I’m looking for something in the Ruby language itself to accomplish this cleanly.

  • 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-11T00:20:52+00:00Added an answer on June 11, 2026 at 12:20 am

    hard to offer a great suggestion without seeing some code, but here are two possibilities.

    1. It sounds like you have two clients for this code. Maybe make it an engine (just a fancy gem), you can add your paths to autoload paths, then use it from the gem without all the railsy crap getting in the way.

    2. Maybe create a constant then reopen it in the models:

    in some initializer

    ActualNamespace = Class.new
    DynamicNamespace = ActualNamespace
    

    in your model file

    class DynamicNamespace
      class MyModel
      end
    end
    
    DynamicNamespace::MyModel # => ActualNamespace::MyModel
    

    Then for your command line app

    DynamicNamespace = Object
    

    Which is the same as not having a namespace:

    DynamicNamespace::MyModel # => MyModel
    

    Now you might wind up having difficulties with some of the Rails magic, which is largely based on reflection. I don’t totally know what you’ll face, but I’d expect forms to start generating the wrong keys when submitting data. You can probably fix this by defining something DynamicNamespace.name or something along those lines.

    Autoloading, is likely to also become an issue, but I think you can declare autoload paths somehow (I don’t know for sure, but googling “rails autoloading” gives some promising results, looks like it just hooks into Ruby’s autoloading — though I think this is going away in Ruby 2.0) worst case, you can probably define a railtie to eager load the dirs for you. This is all a bit out of my league, but I’d assume you need the railties defined before the app is initialized, so you may need to require the railtie in config/application.rb

    Unfortunately, at the end of the day, when you start deviating from Rails conventions, life starts getting hard, and all that magic you never had to think about breaks down so you suddenly have to go diving into the Rails codebase to figure out what it was doing.

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

Sidebar

Related Questions

I have the following models in my Rails application: class Shift < ActiveRecord::Base has_many
I have a rails application that models a house. house contains rooms and rooms
I have a Rails 3 simple scaffolded application having one model like that: class
I have a subscription application with Users, Orders and DeliverySchedules (and other models that
I have a rails application that patches ActiveRecord with a hand-coded validator. The patch
i have a very simple rails application that is ajax-based for some of it's
I have an application that was working fine with ror 2.3.x. I am having
I have a couple of models in a Rails (2.3.11) application that are built
I have a model in my rails application which is class Person < ActiveRecord::Base
I have three models: class Book < ActiveRecord::Base has_many :collections has_many :users, :through =>

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.