mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
5a3247dd8c
Fixes #1305
Before Rails 6 when the build_stubbed strategy assigned an id on a
record without a primary key column, the `id=` method would no-op, and
the record would end up looking like a new_record (i.e. `new_record?`
would return true because the id was nil).
This problem surfaced because of a [change in Rails 6][rails], which
caused `id=` to raise a potentially confusing
`ActiveModel::MissingAttributeError: can't write unknown attribute ''`
for records without a primary key column.
Since build_stubbed stubbed was calling `id=` for all instances,
regardless of whether they had primary keys, it was raising the above
error.
To avoid this error, we check whether the instance has a primary_key
defined before setting the id.
We also changed `persisted?` and `new_record?` to be less dependent on
the id. That way those methods will work as expected for records without
primary keys.
[rails]:
|
||
---|---|---|
.. | ||
attribute | ||
declaration | ||
decorator | ||
strategy | ||
syntax | ||
aliases.rb | ||
attribute.rb | ||
attribute_assigner.rb | ||
attribute_list.rb | ||
callback.rb | ||
callbacks_observer.rb | ||
configuration.rb | ||
declaration.rb | ||
declaration_list.rb | ||
decorator.rb | ||
definition.rb | ||
definition_hierarchy.rb | ||
definition_proxy.rb | ||
errors.rb | ||
evaluation.rb | ||
evaluator.rb | ||
evaluator_class_definer.rb | ||
factory.rb | ||
factory_runner.rb | ||
find_definitions.rb | ||
internal.rb | ||
linter.rb | ||
null_factory.rb | ||
null_object.rb | ||
registry.rb | ||
reload.rb | ||
sequence.rb | ||
strategy_calculator.rb | ||
strategy_syntax_method_registrar.rb | ||
syntax.rb | ||
syntax_runner.rb | ||
trait.rb | ||
version.rb |