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
它引发了一个错误:
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
正如我在示例中看到的,带有条件的lambda应该在关联名称之后执行,因为没有{}
hash只能作为最后一个参数。
尝试
has_many :rates_without_dimension, -> { where(dimension: nil) }, as: :rateable, class_name: "Rate", dependent: :destroy
ps你可以使用http://apidock.com/rails/Object/with_options使它看起来更好
- 如何使用ember-rails gem为HandlebarsJS模板指定备用目录?
- 未初始化的常量MysqlCompat :: MysqlRes(使用mms2r gem)
- 使用回形针保存从api获取的base64图像
- 在Ruby / Rails中PHP的print_r是否相同?
- rails sum多个字段
- 限制使用Google OAuth2.0登录Ruby上的特定白名单域名
- Rails 3:validationIP字符串
- Heroku Rails Net :: HTTP:OpenSSL :: SSL :: SSLError:SSL_connect返回= 1 errno = 0状态= SSLv3读取服务器证书B:证书validation失败
- 嵌套模型测试:找不到表’*’错误。