Save fails due to HATBM relationship... but only if not save

I think this is a pretty weird bug, and would appreciate assistance in
figuring it out.

I have the following model:

class Group < ActiveRecord::Base
has_and_belongs_to_many :managers, :class_name => “User”, :join_table

When I create a new Group object, assign a manager, and save, I get an

$ ruby script/console
Loading development environment.

@group = => ‘test’)
=> #Group:0x4081b908
@group.managers << User.find(1)
=> [#User:0x40b9ba10]!
ActiveRecord::RecordInvalid: Validation failed: Managers is invalid
from (irb):3

However, if I save the new Group object first, then assign the manager,
save again, then it works:

@group = => ‘test_second’)
=> #Group:0x40b75b08!
=> true
@group.managers << User.find(1)
=> [#User:0x40b55830]!
=> true

What could be causing this? Has anybody ran into this problem before?


View this message in context:…-but-only-if-not-saved-before–tf2101944.html#a5792638
Sent from the RubyOnRails Users forum at