Reputation: 453
I have an Order
model with a default attribute status_id
whose default value is 1.
I use rspec
for testing my rails app. I create an object in a describe block as follows:
let!(:order) { create(:order, status_id: 3) }
When I execute the test, I examine the state of the order
object and find that order.status_id
is equal to 3, but when I do Order.last.status_id
I get 1 instead of 3. If I do Order.all.count
I get 1. When I check object ids of order
and Order.last
they are the same. However, I noticed that their memory location is different.
I wonder why am I seeing this behaviour and how can I fix it so that Order.last.status_id
becomes 3 as set in the create
statement?
Upvotes: -1
Views: 474
Reputation: 453
The reason is due to a before_save
callback in the Order
model. I forgot to check there, but now see that when the callback is called it sets the status_id
to the default value based on the value of another attribute.
Upvotes: 0