Rails检查IRB控制台或网页

在我的模型中,我想检查应用程序是在IRB consol内运行还是作为网站运行?

class MyModel < ActiveRecord::Base def xmethod if !isIRBconsol self.user_id = UserSession.find.user.id end end end 

这有点像黑客,但它应该工作:

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

但正如Kathy Van Stone上面所说,这可能是一个更好的解决方案。

为什么不if defined?(IRB)

 unless self.private_methods.include? 'irb_binding' #put your rufus scheduling here end