ruby-on-rails – 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",:conditions => {:dimension => nil}


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

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

  has_one "#{dimension}_average",: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,-> { where(:dimension => nil) }

但它也引发了一个错误,你能指出我有什么问题吗?

解决方法

这里描述的问题相同 https://teamtreehouse.com/forum/unknown-key-conditions

正如我在示例中看到的,带有条件的lambda应该在关联名称后执行,因为没有{}的hash只能作为最后一个参数.

尝试

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

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

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...