小编Ch4*_*Ass的帖子

EventMachine如何在keypress上编写键盘处理程序

我使用EventMachine LineText2协议,我想receive_line每次按下键盘上的字符而不是在我输入新行时触发方法.有没有办法改变默认行为?

class KeyboardHandler < EM::Connection
  include EM::Protocols::LineText2

  def initialize(q)
    @queue = q
  end

  def receive_line(data)
    @queue.push(data)
  end
end

EM.run {
  q = EM::Queue.new

  callback = Proc.new do |line|
    # puts on every keypress not on "\n"
    puts line
    q.pop(&callback)
  end
  q.pop(&callback)

  EM.open_keyboard(KeyboardHandler, q)
}
Run Code Online (Sandbox Code Playgroud)

ruby eventmachine

3
推荐指数
1
解决办法
811
查看次数

ActiveRecord范围不起作用

我有以下关系:

class User < ActiveRecord::Base
  has_many :incoming_delegations,
    :class_name => "Delegation", :foreign_key => :to_user_id,
    :conditions => { :active => true }
  #...
end
Run Code Online (Sandbox Code Playgroud)

class Delegation < ActiveRecord::Base
  belongs_to :from_user, :class_name => "User"
  belongs_to :to_user,   :class_name => "User"

  scope :from, lambda { |user| where(:from_user_id => user.id) }
  scope :to, lambda { |user| where(:to_user_id => user.id) }
  #...
end
Run Code Online (Sandbox Code Playgroud)

现在当我这样做

Delegation.from(User.find(43)) # I get list of delegations from user
User.last.incoming_delegations # I get list of incoming delegations
Run Code Online (Sandbox Code Playgroud)

但是当我尝试:

User.last.incoming_delegations.from(User.find(43))
Run Code Online (Sandbox Code Playgroud)

然后我收到以下错误消息:

TypeError: Cannot …
Run Code Online (Sandbox Code Playgroud)

ruby activerecord ruby-on-rails

1
推荐指数
1
解决办法
442
查看次数

标签 统计

ruby ×2

activerecord ×1

eventmachine ×1

ruby-on-rails ×1