ActiveRecord范围不起作用

时间:2013-04-05 10:57:03

标签: ruby-on-rails ruby activerecord

我有以下关系:

class User < ActiveRecord::Base
  has_many :incoming_delegations,
    :class_name => "Delegation", :foreign_key => :to_user_id,
    :conditions => { :active => true }
  #...
end

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

现在我做的时候

Delegation.from(User.find(43)) # I get list of delegations from user
User.last.incoming_delegations # I get list of incoming delegations

但是当我尝试时:

User.last.incoming_delegations.from(User.find(43))

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

TypeError: Cannot visit User
        from /home/jiri/.rvm/gems/ruby-1.9.3-p194@OM/gems/arel-3.0.2/lib/arel/visitors/visitor.rb:25:in `rescue in visit'
        from /home/jiri/.rvm/gems/ruby-1.9.3-p194@OM/gems/arel-3.0.2/lib/arel/visitors/visitor.rb:19:in `visit'
        from /home/jiri/.rvm/gems/ruby-1.9.3-p194@OM/gems/arel-3.0.2/lib/arel/visitors/to_sql.rb:281:in `visit_Arel_Nodes_JoinSource'
        from /home/jiri/.rvm/gems/ruby-1.9.3-p194@OM/gems/arel-3.0.2/lib/arel/visitors/visitor.rb:19:in `visit'
        from /home/jiri/.rvm/gems/ruby-1.9.3-p194@OM/gems/arel-3.0.2/lib/arel/visitors/to_sql.rb:135:in `visit_Arel_Nodes_SelectCore'
        from /home/jiri/.rvm/gems/ruby-1.9.3-p194@OM/gems/arel-3.0.2/lib/arel/visitors/mysql.rb:41:in `visit_Arel_Nodes_SelectCore'
        from /home/jiri/.rvm/gems/ruby-1.9.3-p194@OM/gems/arel-3.0.2/lib/arel/visitors/to_sql.rb:121:in `block in visit_Arel_Nodes_SelectStatement'
        from /home/jiri/.rvm/gems/ruby-1.9.3-p194@OM/gems/arel-3.0.2/lib/arel/visitors/to_sql.rb:121:in `map'
        from /home/jiri/.rvm/gems/ruby-1.9.3-p194@OM/gems/arel-3.0.2/lib/arel/visitors/to_sql.rb:121:in `visit_Arel_Nodes_SelectStatement'
        from /home/jiri/.rvm/gems/ruby-1.9.3-p194@OM/gems/arel-3.0.2/lib/arel/visitors/mysql.rb:36:in `visit_Arel_Nodes_SelectStatement'
        from /home/jiri/.rvm/gems/ruby-1.9.3-p194@OM/gems/arel-3.0.2/lib/arel/visitors/visitor.rb:19:in `visit'
        from /home/jiri/.rvm/gems/ruby-1.9.3-p194@OM/gems/arel-3.0.2/lib/arel/visitors/visitor.rb:5:in `accept'
        from /home/jiri/.rvm/gems/ruby-1.9.3-p194@OM/gems/arel-3.0.2/lib/arel/visitors/to_sql.rb:19:in `accept'
        from /home/jiri/.rvm/gems/ruby-1.9.3-p194@OM/gems/arel-3.0.2/lib/arel/visitors/bind_visitor.rb:11:in `accept'
        from /home/jiri/.rvm/gems/ruby-1.9.3-p194@OM/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in
        from /home/jiri/.rvm/gems/ruby-1.9.3-p194@OM/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/database_statements.rb:18:i
... 1 levels...

知道导致这个问题的原因是什么?

1 个答案:

答案 0 :(得分:5)

scope :from, lambda { |user| where(:from_user_id => user.id) }

from是范围的错误名称; 将其他名称命名为

当您尝试在:from课程中调用Delegation范围时,正在使用AREL中的

This from method