У меня простая схема с моделями
profile.rb :
class Profile < ApplicationRecord
include AASM
has_many :images, as: :owner, dependent: :destroy
...
image.rb
class Image < ApplicationRecord
belongs_to :owner, polymorphic: true
validates :avatar, uniqueness: { scope: %i[owner_id owner_type] }, allow_blank: true
...
И все работало нормально, и тесты были зелеными, пока я не добавил AASM в класс Profile. После этого следующий тест завершился неудачно со странной ошибкой:
image_spe c .rb
RSpec.describe Image, type: :model do
subject(:image) { create(:image) }
it { should validate_uniqueness_of(:avatar).scoped_to(:owner_id, :owner_type).allow_blank }
end
Ошибка :
Image
is expected to validate that :avatar is case-sensitively unique within the scope of :owner_id and :owner_type as long as it is not blank (FAILED - 1)
Failures:
1) Image is expected to validate that :avatar is case-sensitively unique within the scope of :owner_id and :owner_type as long as it is not blank
Failure/Error: should validate_uniqueness_of(:avatar).scoped_to(:owner_id, :owner_type).allow_blank
NoMethodError:
undefined method `machine_names' for nil:NilClass
# ./spec/models/image_spec.rb:9:in `block (2 levels) in <top (required)>'
Finished in 0.35272 seconds (files took 5.51 seconds to load)
1 example, 1 failure
Я пытался добавить явного владельца для изображения:
image_spe c .rb :
subject(:image) { create(:image, owner: profile) }
let(:profile) { create(:profile) }
, но получил тот же результат. У меня нет machine_names
методов во всем проекте, уже проверено. Он используется в геме AASM. Но я не нашел подобных вопросов ни в inte rnet, ни в репозитории AASM, ни в Shoulda Matchers. Что я делаю не так и как это исправить? Заранее большое спасибо!