Ruby on rails Rails检查IRB控制台或网页

Ruby on rails Rails检查IRB控制台或网页,ruby-on-rails,irb,Ruby On Rails,Irb,在我的模型中,我想检查应用程序是在IRB控制台内运行还是作为网站运行 class MyModel < ActiveRecord::Base def xmethod if !isIRBconsol self.user_id = UserSession.find.user.id end end end classmymodel

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

class MyModel < ActiveRecord::Base
  def xmethod
    if !isIRBconsol
      self.user_id = UserSession.find.user.id
    end
  end
end
classmymodel
这有点像黑客,但应该可以:

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

但正如凯西·范·斯通(Kathy Van Stone)在上文中所说,这可能是一个更好的解决方案。

如果定义了
,为什么不干脆

你能说一下你为什么想要这个吗?较大的问题可能有更好的解决方案。我想在保存之前设置模型的用户id字段。因为IRB中不存在UserSession,所以我尝试这样的smth。更好的解决方案?根据您的范围,这似乎并不总是有效的,我现在选择了
Rails.const_defined?('Console')
unless self.private_methods.include? 'irb_binding'
   #put your rufus scheduling here
end