nested_attributes.rb 23.9 KB
Newer Older
J
Jeremy Kemper 已提交
1 2
require 'active_support/core_ext/hash/except'
require 'active_support/core_ext/object/try'
3
require 'active_support/core_ext/hash/indifferent_access'
J
Jeremy Kemper 已提交
4

5 6
module ActiveRecord
  module NestedAttributes #:nodoc:
7 8 9
    class TooManyRecords < ActiveRecordError
    end

10
    extend ActiveSupport::Concern
11 12

    included do
J
Jon Leighton 已提交
13 14
      class_attribute :nested_attributes_options, instance_writer: false
      self.nested_attributes_options = {}
15 16
    end

R
Rizwan Reza 已提交
17
    # = Active Record Nested Attributes
18 19
    #
    # Nested attributes allow you to save attributes on associated records
H
Hrvoje Šimić 已提交
20 21 22 23
    # through the parent. By default nested attribute updating is turned off
    # and you can enable it using the accepts_nested_attributes_for class
    # method. When you enable nested attributes an attribute writer is
    # defined on the model.
24 25 26
    #
    # The attribute writer is named after the association, which means that
    # in the following example, two new methods are added to your model:
27
    #
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    # <tt>author_attributes=(attributes)</tt> and
    # <tt>pages_attributes=(attributes)</tt>.
    #
    #   class Book < ActiveRecord::Base
    #     has_one :author
    #     has_many :pages
    #
    #     accepts_nested_attributes_for :author, :pages
    #   end
    #
    # Note that the <tt>:autosave</tt> option is automatically enabled on every
    # association that accepts_nested_attributes_for is used for.
    #
    # === One-to-one
    #
    # Consider a Member model that has one Avatar:
    #
    #   class Member < ActiveRecord::Base
    #     has_one :avatar
    #     accepts_nested_attributes_for :avatar
    #   end
    #
    # Enabling nested attributes on a one-to-one association allows you to
    # create the member and avatar in one go:
    #
A
AvnerCohen 已提交
53
    #   params = { member: { name: 'Jack', avatar_attributes: { icon: 'smiling' } } }
P
Pratik Naik 已提交
54
    #   member = Member.create(params[:member])
55 56
    #   member.avatar.id # => 2
    #   member.avatar.icon # => 'smiling'
57 58 59
    #
    # It also allows you to update the avatar through the member:
    #
A
AvnerCohen 已提交
60
    #   params = { member: { avatar_attributes: { id: '2', icon: 'sad' } } }
61
    #   member.update params[:member]
62
    #   member.avatar.icon # => 'sad'
63 64 65 66 67 68 69 70
    #
    # By default you will only be able to set and update attributes on the
    # associated model. If you want to destroy the associated model through the
    # attributes hash, you have to enable it first using the
    # <tt>:allow_destroy</tt> option.
    #
    #   class Member < ActiveRecord::Base
    #     has_one :avatar
A
AvnerCohen 已提交
71
    #     accepts_nested_attributes_for :avatar, allow_destroy: true
72 73
    #   end
    #
74
    # Now, when you add the <tt>_destroy</tt> key to the attributes hash, with a
75 76
    # value that evaluates to +true+, you will destroy the associated model:
    #
A
AvnerCohen 已提交
77
    #   member.avatar_attributes = { id: '2', _destroy: '1' }
78 79
    #   member.avatar.marked_for_destruction? # => true
    #   member.save
80
    #   member.reload.avatar # => nil
81 82 83
    #
    # Note that the model will _not_ be destroyed until the parent is saved.
    #
84
    # Also note that the model will not be destroyed unless you also specify
85
    # its id in the updated hash.
86
    #
87 88 89 90 91 92
    # === One-to-many
    #
    # Consider a member that has a number of posts:
    #
    #   class Member < ActiveRecord::Base
    #     has_many :posts
93
    #     accepts_nested_attributes_for :posts
94 95
    #   end
    #
96 97 98
    # You can now set or update attributes on the associated posts through
    # an attribute hash for a member: include the key +:posts_attributes+
    # with an array of hashes of post attributes as a value.
99
    #
100
    # For each hash that does _not_ have an <tt>id</tt> key a new record will
101
    # be instantiated, unless the hash also contains a <tt>_destroy</tt> key
102 103
    # that evaluates to +true+.
    #
A
AvnerCohen 已提交
104 105 106 107 108
    #   params = { member: {
    #     name: 'joe', posts_attributes: [
    #       { title: 'Kari, the awesome Ruby documentation browser!' },
    #       { title: 'The egalitarian assumption of the modern citizen' },
    #       { title: '', _destroy: '1' } # this will be ignored
109
    #     ]
110 111
    #   }}
    #
112
    #   member = Member.create(params[:member])
113 114 115 116
    #   member.posts.length # => 2
    #   member.posts.first.title # => 'Kari, the awesome Ruby documentation browser!'
    #   member.posts.second.title # => 'The egalitarian assumption of the modern citizen'
    #
117
    # You may also set a +:reject_if+ proc to silently ignore any new record
118 119 120
    # hashes if they fail to pass your criteria. For example, the previous
    # example could be rewritten as:
    #
A
Alexey Muranov 已提交
121 122 123 124
    #   class Member < ActiveRecord::Base
    #     has_many :posts
    #     accepts_nested_attributes_for :posts, reject_if: proc { |attributes| attributes['title'].blank? }
    #   end
125
    #
A
AvnerCohen 已提交
126 127 128 129 130
    #   params = { member: {
    #     name: 'joe', posts_attributes: [
    #       { title: 'Kari, the awesome Ruby documentation browser!' },
    #       { title: 'The egalitarian assumption of the modern citizen' },
    #       { title: '' } # this will be ignored because of the :reject_if proc
131 132 133
    #     ]
    #   }}
    #
134
    #   member = Member.create(params[:member])
135 136 137
    #   member.posts.length # => 2
    #   member.posts.first.title # => 'Kari, the awesome Ruby documentation browser!'
    #   member.posts.second.title # => 'The egalitarian assumption of the modern citizen'
138
    #
139
    # Alternatively, +:reject_if+ also accepts a symbol for using methods:
140
    #
A
Alexey Muranov 已提交
141 142 143 144
    #   class Member < ActiveRecord::Base
    #     has_many :posts
    #     accepts_nested_attributes_for :posts, reject_if: :new_record?
    #   end
145
    #
A
Alexey Muranov 已提交
146 147 148
    #   class Member < ActiveRecord::Base
    #     has_many :posts
    #     accepts_nested_attributes_for :posts, reject_if: :reject_posts
149
    #
B
Benny Klotz 已提交
150 151
    #     def reject_posts(attributes)
    #       attributes['title'].blank?
A
Alexey Muranov 已提交
152 153
    #     end
    #   end
154
    #
155 156
    # If the hash contains an <tt>id</tt> key that matches an already
    # associated record, the matching record will be modified:
157 158
    #
    #   member.attributes = {
A
AvnerCohen 已提交
159 160 161 162
    #     name: 'Joe',
    #     posts_attributes: [
    #       { id: 1, title: '[UPDATED] An, as of yet, undisclosed awesome Ruby documentation browser!' },
    #       { id: 2, title: '[UPDATED] other post' }
163
    #     ]
164 165
    #   }
    #
166 167
    #   member.posts.first.title # => '[UPDATED] An, as of yet, undisclosed awesome Ruby documentation browser!'
    #   member.posts.second.title # => '[UPDATED] other post'
168
    #
169 170 171 172 173
    # However, the above applies if the parent model is being updated as well.
    # For example, If you wanted to create a +member+ named _joe_ and wanted to
    # update the +posts+ at the same time, that would give an
    # ActiveRecord::RecordNotFound error.
    #
174 175 176
    # By default the associated records are protected from being destroyed. If
    # you want to destroy any of the associated records through the attributes
    # hash, you have to enable it first using the <tt>:allow_destroy</tt>
177
    # option. This will allow you to also use the <tt>_destroy</tt> key to
178
    # destroy existing records:
179 180 181
    #
    #   class Member < ActiveRecord::Base
    #     has_many :posts
A
AvnerCohen 已提交
182
    #     accepts_nested_attributes_for :posts, allow_destroy: true
183 184
    #   end
    #
A
AvnerCohen 已提交
185 186
    #   params = { member: {
    #     posts_attributes: [{ id: '2', _destroy: '1' }]
187 188
    #   }}
    #
189
    #   member.attributes = params[:member]
190
    #   member.posts.detect { |p| p.id == 2 }.marked_for_destruction? # => true
191
    #   member.posts.length # => 2
192
    #   member.save
P
Pratik Naik 已提交
193
    #   member.reload.posts.length # => 1
194
    #
195 196 197 198 199 200 201 202 203 204 205 206 207 208
    # Nested attributes for an associated collection can also be passed in
    # the form of a hash of hashes instead of an array of hashes:
    #
    #   Member.create(name:             'joe',
    #                 posts_attributes: { first:  { title: 'Foo' },
    #                                     second: { title: 'Bar' } })
    #
    # has the same effect as
    #
    #   Member.create(name:             'joe',
    #                 posts_attributes: [ { title: 'Foo' },
    #                                     { title: 'Bar' } ])
    #
    # The keys of the hash which is the value for +:posts_attributes+ are
A
Alexey Muranov 已提交
209
    # ignored in this case.
210 211 212 213 214 215
    # However, it is not allowed to use +'id'+ or +:id+ for one of
    # such keys, otherwise the hash will be wrapped in an array and
    # interpreted as an attribute hash for a single post.
    #
    # Passing attributes for an associated collection in the form of a hash
    # of hashes can be used with hashes generated from HTTP/HTML parameters,
W
Waynn Lue 已提交
216
    # where there may be no natural way to submit an array of hashes.
217
    #
218 219 220 221 222
    # === Saving
    #
    # All changes to models, including the destruction of those marked for
    # destruction, are saved and destroyed automatically and atomically when
    # the parent model is saved. This happens inside the transaction initiated
223
    # by the parent's save method. See ActiveRecord::AutosaveAssociation.
224
    #
225 226 227
    # === Validating the presence of a parent model
    #
    # If you want to validate that a child record is associated with a parent
228 229
    # record, you can use the +validates_presence_of+ method and the +:inverse_of+
    # key as this example illustrates:
230 231
    #
    #   class Member < ActiveRecord::Base
A
AvnerCohen 已提交
232
    #     has_many :posts, inverse_of: :member
233 234 235 236
    #     accepts_nested_attributes_for :posts
    #   end
    #
    #   class Post < ActiveRecord::Base
A
AvnerCohen 已提交
237
    #     belongs_to :member, inverse_of: :posts
238 239
    #     validates_presence_of :member
    #   end
240
    #
241
    # Note that if you do not specify the +:inverse_of+ option, then
242 243 244
    # Active Record will try to automatically guess the inverse association
    # based on heuristics.
    #
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
    # For one-to-one nested associations, if you build the new (in-memory)
    # child object yourself before assignment, then this module will not
    # overwrite it, e.g.:
    #
    #   class Member < ActiveRecord::Base
    #     has_one :avatar
    #     accepts_nested_attributes_for :avatar
    #
    #     def avatar
    #       super || build_avatar(width: 200)
    #     end
    #   end
    #
    #   member = Member.new
    #   member.avatar_attributes = {icon: 'sad'}
    #   member.avatar.width # => 200
261
    module ClassMethods
262
      REJECT_ALL_BLANK_PROC = proc { |attributes| attributes.all? { |key, value| key == '_destroy' || value.blank? } }
263

264
      # Defines an attributes writer for the specified association(s).
265 266 267 268
      #
      # Supported options:
      # [:allow_destroy]
      #   If true, destroys any members from the attributes hash with a
269
      #   <tt>_destroy</tt> key and a value that evaluates to +true+
270 271
      #   (eg. 1, '1', true, or 'true'). This option is off by default.
      # [:reject_if]
272 273 274
      #   Allows you to specify a Proc or a Symbol pointing to a method
      #   that checks whether a record should be built for a certain attribute
      #   hash. The hash is passed to the supplied Proc or the method
275
      #   and it should return either +true+ or +false+. When no +:reject_if+
276
      #   is specified, a record will be built for all attribute hashes that
277
      #   do not have a <tt>_destroy</tt> value that evaluates to true.
278
      #   Passing <tt>:all_blank</tt> instead of a Proc will create a proc
279
      #   that will reject a record where all the attributes are blank excluding
280
      #   any value for +_destroy+.
281
      # [:limit]
282 283 284 285 286 287 288 289
      #   Allows you to specify the maximum number of associated records that
      #   can be processed with the nested attributes. Limit also can be specified
      #   as a Proc or a Symbol pointing to a method that should return a number.
      #   If the size of the nested attributes array exceeds the specified limit,
      #   NestedAttributes::TooManyRecords exception is raised. If omitted, any
      #   number of associations can be processed.
      #   Note that the +:limit+ option is only applicable to one-to-many
      #   associations.
290
      # [:update_only]
291
      #   For a one-to-one association, this option allows you to specify how
292
      #   nested attributes are going to be used when an associated record already
293 294
      #   exists. In general, an existing record may either be updated with the
      #   new set of attribute values or be replaced by a wholly new record
295
      #   containing those values. By default the +:update_only+ option is +false+
296 297 298
      #   and the nested attributes are used to update the existing record only
      #   if they include the record's <tt>:id</tt> value. Otherwise a new
      #   record will be instantiated and used to replace the existing one.
299
      #   However if the +:update_only+ option is +true+, the nested attributes
300 301 302
      #   are used to update the record's attributes always, regardless of
      #   whether the <tt>:id</tt> is present. The option is ignored for collection
      #   associations.
303 304
      #
      # Examples:
305
      #   # creates avatar_attributes=
A
AvnerCohen 已提交
306
      #   accepts_nested_attributes_for :avatar, reject_if: proc { |attributes| attributes['name'].blank? }
307
      #   # creates avatar_attributes=
A
AvnerCohen 已提交
308
      #   accepts_nested_attributes_for :avatar, reject_if: :all_blank
309
      #   # creates avatar_attributes= and posts_attributes=
A
AvnerCohen 已提交
310
      #   accepts_nested_attributes_for :avatar, :posts, allow_destroy: true
311
      def accepts_nested_attributes_for(*attr_names)
312
        options = { :allow_destroy => false, :update_only => false }
313
        options.update(attr_names.extract_options!)
314
        options.assert_valid_keys(:allow_destroy, :reject_if, :limit, :update_only)
315
        options[:reject_if] = REJECT_ALL_BLANK_PROC if options[:reject_if] == :all_blank
316 317

        attr_names.each do |association_name|
318
          if reflection = _reflect_on_association(association_name)
319
            reflection.autosave = true
320
            define_autosave_validation_callbacks(reflection)
321 322

            nested_attributes_options = self.nested_attributes_options.dup
323
            nested_attributes_options[association_name.to_sym] = options
324 325
            self.nested_attributes_options = nested_attributes_options

326
            type = (reflection.collection? ? :collection : :one_to_one)
327
            generate_association_writer(association_name, type)
328 329 330 331 332
          else
            raise ArgumentError, "No association found for name `#{association_name}'. Has it been defined yet?"
          end
        end
      end
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347

      private

      # Generates a writer method for this association. Serves as a point for
      # accessing the objects in the association. For example, this method
      # could generate the following:
      #
      #   def pirate_attributes=(attributes)
      #     assign_nested_attributes_for_one_to_one_association(:pirate, attributes)
      #   end
      #
      # This redirects the attempts to write objects in an association through
      # the helper methods defined below. Makes it seem like the nested
      # associations are just regular associations.
      def generate_association_writer(association_name, type)
348
        generated_association_methods.module_eval <<-eoruby, __FILE__, __LINE__ + 1
349 350 351 352 353 354 355 356
          if method_defined?(:#{association_name}_attributes=)
            remove_method(:#{association_name}_attributes=)
          end
          def #{association_name}_attributes=(attributes)
            assign_nested_attributes_for_#{type}_association(:#{association_name}, attributes)
          end
        eoruby
      end
357 358
    end

359 360 361
    # Returns ActiveRecord::AutosaveAssociation::marked_for_destruction? It's
    # used in conjunction with fields_for to build a form element for the
    # destruction of this association.
362 363
    #
    # See ActionView::Helpers::FormHelper::fields_for for more info.
364
    def _destroy
365 366 367 368 369
      marked_for_destruction?
    end

    private

370 371
    # Attribute hash keys that should not be assigned as normal attributes.
    # These hash keys are nested attributes implementation details.
372
    UNASSIGNABLE_KEYS = %w( id _destroy )
373 374 375

    # Assigns the given attributes to the association.
    #
376 377 378 379 380 381 382
    # If an associated record does not yet exist, one will be instantiated. If
    # an associated record already exists, the method's behavior depends on
    # the value of the update_only option. If update_only is +false+ and the
    # given attributes include an <tt>:id</tt> that matches the existing record's
    # id, then the existing record will be modified. If no <tt>:id</tt> is provided
    # it will be replaced with a new record. If update_only is +true+ the existing
    # record will be modified regardless of whether an <tt>:id</tt> is provided.
383
    #
384 385 386
    # If the given attributes include a matching <tt>:id</tt> attribute, or
    # update_only is true, and a <tt>:_destroy</tt> key set to a truthy value,
    # then the existing record will be marked for destruction.
387
    def assign_nested_attributes_for_one_to_one_association(association_name, attributes)
388
      options = self.nested_attributes_options[association_name]
389 390 391
      if attributes.respond_to?(:permitted?)
        attributes = attributes.to_h
      end
392
      attributes = attributes.with_indifferent_access
393
      existing_record = send(association_name)
394

395 396 397
      if (options[:update_only] || !attributes['id'].blank?) && existing_record &&
          (options[:update_only] || existing_record.id.to_s == attributes['id'].to_s)
        assign_to_or_mark_for_destruction(existing_record, attributes, options[:allow_destroy]) unless call_reject_if(association_name, attributes)
398

399
      elsif attributes['id'].present?
400
        raise_nested_attributes_record_not_found!(association_name, attributes['id'])
401

402
      elsif !reject_new_record?(association_name, attributes)
403 404 405 406 407
        assignable_attributes = attributes.except(*UNASSIGNABLE_KEYS)

        if existing_record && existing_record.new_record?
          existing_record.assign_attributes(assignable_attributes)
          association(association_name).initialize_attributes(existing_record)
408
        else
409 410 411 412 413 414
          method = "build_#{association_name}"
          if respond_to?(method)
            send(method, assignable_attributes)
          else
            raise ArgumentError, "Cannot build association `#{association_name}'. Are you trying to build a polymorphic one-to-one association?"
          end
415
        end
416 417 418 419 420
      end
    end

    # Assigns the given attributes to the collection association.
    #
421 422 423
    # Hashes with an <tt>:id</tt> value matching an existing associated record
    # will update that record. Hashes without an <tt>:id</tt> value will build
    # a new record for the association. Hashes with a matching <tt>:id</tt>
424
    # value and a <tt>:_destroy</tt> key set to a truthy value will mark the
425
    # matched record for destruction.
426 427 428 429
    #
    # For example:
    #
    #   assign_nested_attributes_for_collection_association(:people, {
A
AvnerCohen 已提交
430 431 432
    #     '1' => { id: '1', name: 'Peter' },
    #     '2' => { name: 'John' },
    #     '3' => { id: '2', _destroy: true }
433 434
    #   })
    #
435
    # Will update the name of the Person with ID 1, build a new associated
436
    # person with the name 'John', and mark the associated Person with ID 2
437 438 439 440 441
    # for destruction.
    #
    # Also accepts an Array of attribute hashes:
    #
    #   assign_nested_attributes_for_collection_association(:people, [
A
AvnerCohen 已提交
442 443 444
    #     { id: '1', name: 'Peter' },
    #     { name: 'John' },
    #     { id: '2', _destroy: true }
445
    #   ])
446
    def assign_nested_attributes_for_collection_association(association_name, attributes_collection)
447
      options = self.nested_attributes_options[association_name]
448 449 450
      if attributes_collection.respond_to?(:permitted?)
        attributes_collection = attributes_collection.to_h
      end
451

452 453
      unless attributes_collection.is_a?(Hash) || attributes_collection.is_a?(Array)
        raise ArgumentError, "Hash or Array expected, got #{attributes_collection.class.name} (#{attributes_collection.inspect})"
454 455
      end

456
      check_record_limit!(options[:limit], attributes_collection)
457

458
      if attributes_collection.is_a? Hash
459 460
        keys = attributes_collection.keys
        attributes_collection = if keys.include?('id') || keys.include?(:id)
461
          [attributes_collection]
462
        else
A
Aaron Patterson 已提交
463
          attributes_collection.values
464
        end
465 466
      end

467
      association = association(association_name)
468 469

      existing_records = if association.loaded?
470
        association.target
471 472
      else
        attribute_ids = attributes_collection.map {|a| a['id'] || a[:id] }.compact
J
Jon Leighton 已提交
473
        attribute_ids.empty? ? [] : association.scope.where(association.klass.primary_key => attribute_ids)
474 475
      end

476
      attributes_collection.each do |attributes|
477 478 479
        if attributes.respond_to?(:permitted?)
          attributes = attributes.to_h
        end
480
        attributes = attributes.with_indifferent_access
481

482 483
        if attributes['id'].blank?
          unless reject_new_record?(association_name, attributes)
484
            association.build(attributes.except(*UNASSIGNABLE_KEYS))
485
          end
486
        elsif existing_record = existing_records.detect { |record| record.id.to_s == attributes['id'].to_s }
487 488 489 490 491 492 493 494 495 496
          unless call_reject_if(association_name, attributes)
            # Make sure we are operating on the actual object which is in the association's
            # proxy_target array (either by finding it, or adding it if not found)
            # Take into account that the proxy_target may have changed due to callbacks
            target_record = association.target.detect { |record| record.id.to_s == attributes['id'].to_s }
            if target_record
              existing_record = target_record
            else
              association.add_to_target(existing_record, :skip_callbacks)
            end
497

498
            assign_to_or_mark_for_destruction(existing_record, attributes, options[:allow_destroy])
499
          end
500
        else
501
          raise_nested_attributes_record_not_found!(association_name, attributes['id'])
502
        end
503 504 505
      end
    end

506
    # Takes in a limit and checks if the attributes_collection has too many
507 508
    # records. It accepts limit in the form of symbol, proc, or
    # number-like object (anything that can be compared with an integer).
509
    #
510
    # Raises TooManyRecords error if the attributes_collection is
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
    # larger than the limit.
    def check_record_limit!(limit, attributes_collection)
      if limit
        limit = case limit
        when Symbol
          send(limit)
        when Proc
          limit.call
        else
          limit
        end

        if limit && attributes_collection.size > limit
          raise TooManyRecords, "Maximum #{limit} records are allowed. Got #{attributes_collection.size} records instead."
        end
      end
    end

529
    # Updates a record with the +attributes+ or marks it for destruction if
530
    # +allow_destroy+ is +true+ and has_destroy_flag? returns +true+.
531 532
    def assign_to_or_mark_for_destruction(record, attributes, allow_destroy)
      record.assign_attributes(attributes.except(*UNASSIGNABLE_KEYS))
533
      record.mark_for_destruction if has_destroy_flag?(attributes) && allow_destroy
534
    end
535

536 537
    # Determines if a hash contains a truthy _destroy key.
    def has_destroy_flag?(hash)
S
Sean Griffin 已提交
538
      Type::Boolean.new.cast(hash['_destroy'])
539 540
    end

541
    # Determines if a new record should be rejected by checking
542
    # has_destroy_flag? or if a <tt>:reject_if</tt> proc exists for this
543 544
    # association and evaluates to +true+.
    def reject_new_record?(association_name, attributes)
545 546 547
      has_destroy_flag?(attributes) || call_reject_if(association_name, attributes)
    end

548 549 550 551 552
    # Determines if a record with the particular +attributes+ should be
    # rejected by calling the reject_if Symbol or Proc (if defined).
    # The reject_if option is defined by +accepts_nested_attributes_for+.
    #
    # Returns false if there is a +destroy_flag+ on the attributes.
553
    def call_reject_if(association_name, attributes)
554
      return false if has_destroy_flag?(attributes)
555
      case callback = self.nested_attributes_options[association_name][:reject_if]
556 557 558
      when Symbol
        method(callback).arity == 0 ? send(callback) : send(callback, attributes)
      when Proc
559
        callback.call(attributes)
560
      end
561
    end
562

563
    def raise_nested_attributes_record_not_found!(association_name, record_id)
564 565 566
      model = self.class._reflect_on_association(association_name).klass.name
      raise RecordNotFound.new("Couldn't find #{model} with ID=#{record_id} for #{self.class.name} with ID=#{id}",
                               model, 'id', record_id)
567
    end
568
  end
569
end