2014-07-08 51 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使其看起來更好