mongoid/mongoid — Issue #1530: Manual updates of HABTM ids fail after atomic_array_pushes
Author: fwoeck
Created: 2011-12-28 13:09:00+00:00
State: open

Hi guys!

Please consider the following spec against the latest master:

```ruby
context "when relational ids are changed manually" do

let(:person) do
Person.create(:ssn => "342-89-2439")
end

let(:preference) do
Preference.create(:name => "Test")
end

it "should contain unique id arrays" do
person.preference_ids = [preference.id]
preference.person_ids = [person.id]

person.save
preference.save

preference.person_ids.should == [person.id]
preference.reload.person_ids.should == [person.id]
end

end
```

It fails in the last expectation because the "person.save"-operation persisted the person.id for "preference". So the preference.reload.person_ids equals actually [person.id, person.id].

Of course the regular way of adding a relational object would be preference.people << person, but let's say I cannot use this for various reasons and I have to execute a .save on each relational partner.

Is there a non-trivial way to make this spec work?
Maniulations of update_inverse_keys(meta) weren't successful.

I replaced lib/mongoid/dirty.rb line 121

if pushes.any? && pulls.any?
by
if (pushes.any? && pulls.any?) || name[/_ids$/]

but this is obviously hacky.

Thanks in advance,
Frank