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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:48:28+00:00 2026-05-26T10:48:28+00:00

I have a case where I’m trying to leverage a database configuration file used

  • 0

I have a case where I’m trying to leverage a database configuration file used by Padrino for a custom worker I’m writing without having to load the Padrino environment and without having to modify the existing database configuration. In short, I want to write my code to work with the existing database configuration code rather than modifying the existing configuration.

The database configuration file in question looks like the following:

# This is just here for right now so I can test
# to see if logger is set to anything in DB config
puts "Logger in DB config: #{logger}"

ActiveRecord::Base.configurations[:development] = {
  :adapter => 'sqlite3',
  :database => Padrino.root('db', "app_development.db") }
ActiveRecord::Base.logger = logger
ActiveRecord::Base.include_root_in_json = true
ActiveRecord::Base.store_full_sti_class = true
ActiveSupport.use_standard_json_time_format = true
ActiveSupport.escape_html_entities_in_json = false
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[Padrino.env])

My custom worker looks like the following:

ROOT = File.expand_path(File.dirname(__FILE__))
$LOAD_PATH.unshift(ROOT)

require 'active_record'
require 'logger'

# Mock Padrino module with methods used by database config file
module Padrino
  def self.root(*args)
    return File.expand_path(File.join("#{ROOT}/..", *args))
end

  def self.env
    return :development
  end
end

module Worker
  class << self
    attr_accessor :logger

    def init
      @logger       = Logger.new(STDOUT)
      @logger.level = Logger::DEBUG

      require "#{ROOT}/../config/database"
    end
  end
end

Worker.init

When I require the database configuration file in my worker I get an error saying the following:

/home/user/devel/app/config/database.rb:3:in `<top (required)>': undefined local variable or method `logger' for main:Object (NameError)
from /home/user/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:55:in `require'
from /home/user/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:55:in `require'
from worker.rb:26:in `init'
from worker.rb:31:in `<main>'

Given this, I modified the Worker#init function to be the following:

def init
  @logger       = Logger.new(STDOUT)
  @logger.level = Logger::DEBUG

  Object.send(:define_method, :logger, lambda { @logger })

  puts "Logger in Worker module: #{logger}"

  require "#{ROOT}/../config/database"
end

This change resulted in the following output:

Logger in Worker module: #<Logger:0x9505088>
Logger in DB config:

I take this to mean the @logger is not in scope in the database configuration file, even though I’m using a lambda when I define the logger method on Object.

Oddly enough, if I pull the Object.send line of code out of the Worker#init function and instead call it right before I call Worker.init like below, I get the following result.

Object.send(:define_method, :logger, lambda { Worker.logger })
Worker.init

results in

Logger in Worker module: #<Logger:0x81bc8b4>
Logger in DB config: #<Logger:0x81bc8b4>

Can someone explain to me why if I make the Object.send call inside the Worker#init function it doesn’t work like it does if I make it outside the Worker module?

  • 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-26T10:48:28+00:00Added an answer on May 26, 2026 at 10:48 am

    In your first example, you try to use undefined variable ‘logger’, as you can see from error message. Try to use the logger feature this way:

    ActiveRecord::Base.logger = Logger.new(STDOUT)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a case where I wanna choose any database entry that have an
I have case where I want a database user to have db_owner privileges with
I'm currently working with the versant object database (using jvi), and have a case
I have a case where some values in a data file have 64 bit
I have a case in which I have to read an input file in
I have this case where I am using a custom UITabBar in my application.
I have a case where on my view file there are 6 links and
I have a case where a 3rd party ad is bleeding through my modal
I have a case that keeps coming up where I'm using a ListView or
I have a case where if a SharePoint site owner decides to break permissions

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.