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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:22:23+00:00 2026-05-25T23:22:23+00:00

I have a Rails 3 app with a presenter that lives in lib .

  • 0

I have a Rails 3 app with a presenter that lives in lib. The relevant part looks like:

lib/channels/channel.rb:

module Channels
  class Channel
    def current_user
      ApplicationController.current_controller.try(:current_or_guest_user)
    end

    def self.find_by_key(key)
      @@channels.find { |c| c.key == key.to_sym }
    end

    private

    def self.class_initialize
      @@channels = []
      Dir.glob("#{Rails.root}/lib/channels/channel_defs/*.rb").each do |f|
        require_dependency f
        @@channels << "Channels::#{File.basename(f, '.rb').camelize}".constantize.new
      end
    end

    class_initialize
  end
end

lib/channels/channel_defs/activity.rb

module Channels
  class Activity

    def current_user
      ApplicationController.current_controller.try(:current_or_guest_user)
    end

    def accessible?
      current_user.registered_user?
    end
  end
end

ApplicationController.current_controller is a hack so our presenter can get to helpers; we set it to self in a before_filter. This of course doesn’t work in console, and I want to be able to work with Channel.accessible? in console, so I tried overriding the method like so in .irbrc:

module Channels
  class Channel
    class << self
      puts "in irbrc"
      def current_user
        User.find(475)
      end
    end
  end
end

And it seems to work when called directly:

ruby-1.9.2-p290 :002 > Channels::Channel.current_user
=> #

But not when called from a channel itself:

Channels::Channel.find_by_key(:activity).accessible?NoMethodError: undefined method `registered_user?' for nil:NilClass
    from /Users/jay/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.9/lib/active_support/whiny_nil.rb:48:in `method_missing'
    from /Users/jay/src/tiptap/2t2/lib/channels/channel_defs/activity.rb:16:in `accessible?'
    from (irb):1

This is probably either something about the eigenclass vs the class, or something about the timing of loading Channels::Channel. I’ve tried adding a require channels/channel in .irbrc before we reopen the class, but that doesn’t do it… ideas?

  • 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-25T23:22:24+00:00Added an answer on May 25, 2026 at 11:22 pm

    In development mode the model classes are defined as temporary copies and re-defined on reload! which can make extensions like this unreliable or ineffective.

    It’s better to layer this sort of hackery in as some kind of configuration option. For instance:

    def current_user
      ENV['USER_ID'] ? User.find(ENV['USER_ID']) : ApplicationController.current_controller.try(:current_or_guest_user)
    end
    

    This way you can start up with any user you’d like:

    USER_ID=475 rails console
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rails app with some nested data that I'd like to export
I have a simple rails app that is like an event system which has
I have a rails app that has asynchronous processing, and I'm having trouble getting
I have a Rails app that needs to expose values from a database as
I have a Rails app that archives high-res images (and thumbs) on S3 (via
We have a Rails app that instantiates a SWF object 16 times (it has
I have a rails app that I'm moving to another server and I figure
I have some additional scripts that I've written outside of my Rails 3.1.x app,
I have the following class in a Rails 3.1.1 app: class User < ActiveRecord::Base
I have a rails app that has picked up a bit of traction, but

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.