gpt4 book ai didi

ruby-on-rails - Rails 关联中的未知键

转载 作者:行者123 更新时间:2023-12-04 07:22:23 24 4
gpt4 key购买 nike

我有以下关联代码:

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 应该在关联名称之后执行,因为没有 {} 的哈希只能作为最后一个论点。

尝试

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

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

关于ruby-on-rails - Rails 关联中的未知键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24621827/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com