我正在从Rails 3迁移到Rails 4,并且其中一个模型的以下关联导致错误:
has_many :unblocked_items, class_name: 'IncidentItem',
conditions: [
'incident_items.item_type_id in (?) \
AND (incident_blocking_file_id IS NULL OR incident_blocking_files.way = ? \
)',
IncidentItemType.blockable,
BlockingWay.unblock
],
include: :incident_blocking_file
has_many :blocked_items, class_name: 'IncidentItem',
conditions: [
'incident_items.item_type_id in (?) \
AND (incident_blocking_file_id IS NOT NULL \
AND (incident_blocking_files.way <> ? OR incident_blocking_files.way IS NULL) \
)',
IncidentItemType.blockable,
BlockingWay.unblock
],
include: :incident_blocking_file
我收到以下错误:
ArgumentError: Unknown key: :conditions. Valid keys are: :class_name, :anonymous_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, :join_table, :foreign_type (ArgumentError)
什么原因导致此错误?如何将这个模型及其关联从Rails 3迁移到Rails 4?
答案 0 :(得分:1)
来自:https://edgeguides.rubyonrails.org/association_basics.html#scopes-for-has-many
4.3.3 has_many的范围
有时您可能希望自定义has_many使用的查询。可以通过范围框实现此类自定义。例如:
class Author < ApplicationRecord
has_many :books, -> { where processed: true }
end