Cannot use transients with Mongoid
Closed this issue · 4 comments
If I add a transient in a Fabricator I have the next error:
Fabricator(:user) do
transient something: 0
end
Fabricate(:user)
Mongoid::Errors::UnknownAttribute:
Problem:
Attempted to set a value for 'something' which is not allowed on the model User.
Summary:
Without including Mongoid::Attributes::Dynamic in your model and the attribute does not already exist in the attributes hash, attempting to call User#contact_count= for it is not allowed. This is also triggered by passing the attribute to any method that accepts an attri
butes hash, and is raised instead of getting a NoMethodError. Resolution:
You can include Mongoid::Attributes::Dynamic if you expect to be writing values for undefined fields often.
from /home/alex/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/mongoid-4.0.2/lib/mongoid/attributes/processing.rb:96:in `process_attribute'
The only solution I found is to add the Mongoid::Attributes::Dynamic
module in the user class, but it is not a good solution for me because I don't want that behaviour.
Thanks in advance!
That's odd. Transients are processed in the attributes hash and removed before the underlying objects are created.
What version of fabrication are you using?
@paulelliott Using version 2.13.2
I've tried to reproduce this issue in the test suite and have not been able to. Have you made any progress on your end? If this is still an issue, do you think you could open a PR with a failing test to demonstrate the issue?
I tried to reproduce this in other projects and I couldn't. It has to be something very specific of this project. If I found the reason I open this again. Thank you for your time :)