-
Type: Bug
-
Resolution: Duplicate
-
Priority: Major - P3
-
None
-
Affects Version/s: 6.1.0
-
Component/s: None
-
None
class Group include Mongoid::Document embeds_many :users, cascade_callbacks: true end class User include Mongoid::Document field :email, type: String field :email_md5, type: String embedded_in :group before_save :calculate_md5 def calculate_md5 self.email_md5 = Digest::MD5.hexdigest(email) end end require 'rails_helper' RSpec.describe Group, type: :model do before do @group= Group.new(users: [{ email: 'test' }]) @group.save end it 'saves email_md5 on create' do email_md5 = @group.reload.users[0].email_md5 expect(email_md5).to eq Digest::MD5.hexdigest('test') end describe 'on update' do before do @group.update(users: [{ email: 'test2' }]) end it 'calculate email_md5' do email_md5 = @group.users[0].email_md5 expect(email_md5).to eq Digest::MD5.hexdigest('test2') end it 'actually saves email' do email = @group.reload.users[0].email expect(email).to eq 'test2' end it 'actually saves email_md5' do email_md5 = @group.reload.users[0].email_md5 expect(email_md5).not_to be_blank end end end
The last test fails. It does call the callback but not actually saves it to the database.
https://github.com/aptx4869/dummy
- duplicates
-
MONGOID-2306 cascade_callbacks not working using before_create and <<
- Closed
-
MONGOID-3648 Inconsistent behaviour for embedded objects callbacks
- Closed
-
MONGOID-3795 Clarify/improve order of callback invocation on embedded associations vs host documents
- Closed