1

I am migrating from Rails 3 to Rails 4 and the following associations from one of my models is causing an error:

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

I get the following error:

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)

What causes this error? How can I migrate this model and its associations from Rails 3 to Rails 4?

anothermh
  • 9,815
  • 3
  • 33
  • 52
anquegi
  • 11,125
  • 4
  • 51
  • 67
  • Possible duplicate of [What is the equivalent of the has\_many 'conditions' option in Rails 4?](https://stackoverflow.com/questions/20307874/what-is-the-equivalent-of-the-has-many-conditions-option-in-rails-4) – Getu Oct 24 '18 at 14:55
  • What version of Ruby on Rails are you actually using? Are you still on Rails 3 or 4 - like the tags you added? Or are you on an up-to-date version? You cannot use two versions at the same time, so what version exactly? – spickermann Oct 24 '18 at 15:25
  • I'm migrating to rails 4 – anquegi Oct 24 '18 at 16:11

1 Answers1

1

from: https://edgeguides.rubyonrails.org/association_basics.html#scopes-for-has-many

4.3.3 Scopes for has_many

There may be times when you wish to customize the query used by has_many. Such customizations can be achieved via a scope block. For example:

class Author < ApplicationRecord
  has_many :books, -> { where processed: true }
end
Kache
  • 15,647
  • 12
  • 51
  • 79
Mark
  • 6,112
  • 4
  • 21
  • 46