2014-07-08 26 views
8

我有以下协会代码:未知关键

has_many :rates_without_dimension, :as => :rateable, :class_name => "Rate", 
    :dependent => :destroy, :conditions => {:dimension => nil} 
has_many :raters_without_dimension, :through => :rates_without_dimension, 
    :source => :rater 

has_one :rate_average_without_dimension, :as => :cacheable, 
    :class_name => "RatingCache", 
:dependent => :destroy, :conditions => {:dimension => nil} 


dimensions.each do |dimension|   
    has_many "#{dimension}_rates", :dependent => :destroy, 
    :conditions => {:dimension => dimension.to_s}, 
    :class_name => "Rate", 
    :as => :rateable 

    has_many "#{dimension}_raters", :through => "#{dimension}_rates", 
    :source => :rater   

    has_one "#{dimension}_average", :as => :cacheable, :class_name => "RatingCache", 
    :dependent => :destroy, :conditions => {:dimension => dimension.to_s} 
end 

它提出了一个错误:

Unknown key: :conditions. Valid keys are: :class_name, :class, :foreign_key, :validate, :autosave, :table_name, :before_add, :after_add, :before_remove, :after_remove, :extend, :primary_key, :dependent, :as, :through, :source, :source_type, :inverse_of, :counter_cache 

我想的第一行变成:

has_many :rates_without_dimension, :as => :rateable, :class_name => "Rate", :dependent => :destroy,-> { where(:dimension => nil) } 

但它也提出了一个错误,你能指出我有什么问题吗?这里https://teamtreehouse.com/forum/unknown-key-conditions

描述

+2

编辑该问题以包含来自外部网站的源代码。从[SO帮助](http://stackoverflow.com/help/how-to-ask):“如果可以创建一个可以链接到的问题的实例(例如http:// sqlfiddle.com/或http://jsbin.com/),然后这样做 - 但也包括代码在您的问题本身。不是每个人都可以访问外部网站,并且链接可能会随着时间的推移而中断。“ – Amadan

+0

感谢您的编辑和引用:) –

+0

使用 - >符号的错误是什么? – JTG

回答

9

同样的问题我在例子中看到,拉姆达与条件应没有关联的名字后,因为没有{}哈希只能作为最后一个参数。

尝试

has_many :rates_without_dimension, -> { where(dimension: nil) }, as: :rateable, class_name: "Rate", dependent: :destroy 

附:您可以使用http://apidock.com/rails/Object/with_options使其看起来更好