2014-07-08 12 views
8

Tôi có đoạn code công ty sau đây:chính Unknown gắn Rails

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 

Nó đặt ra một lỗi:

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 

Tôi cố gắng để thay đổi dòng đầu tiên thành:

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

Nhưng nó cũng đưa ra một lỗi, bạn có thể chỉ cho tôi những gì là sai với nó?

+2

Đã chỉnh sửa câu hỏi để bao gồm mã nguồn từ một trang web bên ngoài. Từ [SO help] (http://stackoverflow.com/help/how-to-ask): "Nếu có thể tạo ví dụ trực tiếp về sự cố mà bạn có thể liên kết tới (ví dụ: trên http: // sqlfiddle.com/ hoặc http://jsbin.com/) sau đó làm như vậy - nhưng cũng bao gồm mã trong chính câu hỏi của bạn. Không phải ai cũng có thể truy cập các trang web bên ngoài và liên kết có thể bị hỏng theo thời gian ". – Amadan

+0

Cảm ơn bạn đã chỉnh sửa và báo giá :) –

+0

Lỗi bằng ký hiệu -> là gì? – JTG

Trả lời

9

Cùng một vấn đề được mô tả ở đây https://teamtreehouse.com/forum/unknown-key-conditions

Như tôi đã nhìn thấy trong ví dụ, lambda với điều kiện nên không sau tên hiệp hội, vì băm mà không {} thể chỉ có lập luận như trước.

Hãy thử

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

tái bút: bạn có thể sử dụng http://apidock.com/rails/Object/with_options để làm cho nó trông đẹp hơn

Các vấn đề liên quan