2013-04-05 106 views
1

我有如下關係: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/[email protected]/gems/arel-3.0.2/lib/arel/visitors/visitor.rb:25:in `rescue in visit' 
     from /home/jiri/.rvm/gems/[email protected]/gems/arel-3.0.2/lib/arel/visitors/visitor.rb:19:in `visit' 
     from /home/jiri/.rvm/gems/[email protected]/gems/arel-3.0.2/lib/arel/visitors/to_sql.rb:281:in `visit_Arel_Nodes_JoinSource' 
     from /home/jiri/.rvm/gems/[email protected]/gems/arel-3.0.2/lib/arel/visitors/visitor.rb:19:in `visit' 
     from /home/jiri/.rvm/gems/[email protected]/gems/arel-3.0.2/lib/arel/visitors/to_sql.rb:135:in `visit_Arel_Nodes_SelectCore' 
     from /home/jiri/.rvm/gems/[email protected]/gems/arel-3.0.2/lib/arel/visitors/mysql.rb:41:in `visit_Arel_Nodes_SelectCore' 
     from /home/jiri/.rvm/gems/[email protected]/gems/arel-3.0.2/lib/arel/visitors/to_sql.rb:121:in `block in visit_Arel_Nodes_SelectStatement' 
     from /home/jiri/.rvm/gems/[email protected]/gems/arel-3.0.2/lib/arel/visitors/to_sql.rb:121:in `map' 
     from /home/jiri/.rvm/gems/[email protected]/gems/arel-3.0.2/lib/arel/visitors/to_sql.rb:121:in `visit_Arel_Nodes_SelectStatement' 
     from /home/jiri/.rvm/gems/[email protected]/gems/arel-3.0.2/lib/arel/visitors/mysql.rb:36:in `visit_Arel_Nodes_SelectStatement' 
     from /home/jiri/.rvm/gems/[email protected]/gems/arel-3.0.2/lib/arel/visitors/visitor.rb:19:in `visit' 
     from /home/jiri/.rvm/gems/[email protected]/gems/arel-3.0.2/lib/arel/visitors/visitor.rb:5:in `accept' 
     from /home/jiri/.rvm/gems/[email protected]/gems/arel-3.0.2/lib/arel/visitors/to_sql.rb:19:in `accept' 
     from /home/jiri/.rvm/gems/[email protected]/gems/arel-3.0.2/lib/arel/visitors/bind_visitor.rb:11:in `accept' 
     from /home/jiri/.rvm/gems/[email protected]/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in 
     from /home/jiri/.rvm/gems/[email protected]/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/database_statements.rb:18:i 
... 1 levels... 

任何想法是什麼導致這個問題呢?

回答

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

from對於範圍來說是一個壞名字; 把它命名爲別的東西

This from method內AREL被當你試圖打電話給你Delegation:from範圍使用。