Rails check if IRB console or webpage
Asked Answered
L

3

5

In my model I would like to check if the app is running inside IRB consol or as a website?

class MyModel < ActiveRecord::Base
  def xmethod
    if !isIRBconsol
      self.user_id = UserSession.find.user.id
    end
  end
end
Lashaunda answered 24/2, 2010 at 21:35 Comment(2)
Can you say why you want this? The larger problem might have a better solution.Phyllys
I want to set user_id field of a model before_save. Because UserSession does not exists in IRB i try smth like that. Better solutiom?Lashaunda
A
4

Why not just if defined?(IRB)?

Avivah answered 4/8, 2011 at 20:13 Comment(1)
This doesn't seem to always work depending on your scope, I've settled on Rails.const_defined?('Console') for now.Multitude
C
3

This is a bit of a hack, but it should work:

class MyModel < ActiveRecord::Base
  def am_i_in_irb?
    self.private_methods.include? 'irb_binding'
  end
end

But as Kathy Van Stone said above, this is probably something that has a better solution.

Cadge answered 24/2, 2010 at 22:30 Comment(0)
C
0
unless self.private_methods.include? 'irb_binding'
   #put your rufus scheduling here
end
Cornwall answered 17/2, 2016 at 10:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.