class User < ActiveRecord::Base serialize :group_ids, Array end
1 2 3
> User.create(name: 'bob', group_ids: '1,2,3') (0.1ms) begin transaction (0.1ms) rollback transactionActiveRecord::SerializationTypeMismatch: Attribute was supposed to be a Array, but was a String. -- "1,2,3
describe '.serialize' do let(:user) { build :user }
context 'when group_ids given the Array object' do subject { user.group_ids } before do user.group_ids = [1, 3, 5] user.save end it { should match_array [1,3,5] } end
context 'when group_ids given an invalid object' do [12345, 'string value', {key: 'value'}].each do |invalid_object| before { user.group_ids = invalid_object } it { expect { user.save }.to raise_error(ActiveRecord::SerializationTypeMismatch) } end end end