Unknown key in Rails association

I have the following associations code:

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 

An error occurs:

 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 

I tried changing the first line to:

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

But it also caused an error, can you tell me what is wrong with her?

+8
ruby-on-rails ruby-on-rails-4
source share
1 answer

Same issue described here https://teamtreehouse.com/forum/unknown-key-conditions

As I see in the examples, a lambda with a condition should be done after the association name, because a hash without {} can only be the last argument.

Try

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

ps you can use http://apidock.com/rails/Object/with_options to make it look better

+9
source share

All Articles