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
=>
‘groups_managers’
end

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

$ ruby script/console
Loading development environment.

@group = Group.new(:name => ‘test’)
=> #Group:0x4081b908

@group.managers << User.find(1)
=> [#User:0x40b9ba10]

@group.save!
ActiveRecord::RecordInvalid: Validation failed: Managers is invalid
from
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/validations.rb:748:in
`save!’
from (irb):3

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

@group = Group.new(:name => ‘test_second’)
=> #Group:0x40b75b08

@group.save!
=> true

@group.managers << User.find(1)
=> [#User:0x40b55830]

@group.save!
=> true

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

Thanks!

View this message in context:
http://www.nabble.com/Save-fails-due-to-HATBM-relationship…-but-only-if-not-saved-before–tf2101944.html#a5792638
Sent from the RubyOnRails Users forum at Nabble.com.