Forum: Ruby on Rails habtm doesn't save old IDs

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
5224d56434a3d237408c217605df8a56?d=identicon&s=25 Payton Swick (Guest)
on 2006-02-12 20:23
(Received via mailing list)
I'm sure I'm not getting something.  I have Users, and I have Groups.
Each User has_and_belongs_to_many :groups and each Group
has_and_belongs_to_many :users.

When I add a user, I want to apply a bunch of groups (like tags) to the
user (manually, without acts_as_taggable).

Here's User#create:

@user =[:user])
     @user.groups = params[:user][:group_names].split(/\s+/).collect do
       Group.find(:first, :conditions => ["name = ?", group]) ||{:name => group})

Groups that have not yet been created are created and their associations
to the user are saved in groups_users.  Groups that already exist,
however, are *not* added to groups_users.

The further oddity is that User#groups=() does add previously existing
Groups, *if* I call it on a User object that has already been added to
the database.

Here's an entry from the logs:

before save, groups=[#<Group:0x40710a9c @attributes={"name"=>"one",
"id"=>"1"}>, #<Group:0x4066e654 @new_record=true,

This Group gets added:
#<Group:0x4066e654 @new_record=true, @attributes={"name"=>"eight"}>

This Group does not:
#<Group:0x40710a9c @attributes={"name"=>"one", "id"=>"1"}>

Why? Validation? I checked and there's no errors before or after the

Eac107645eb8b8198558af8af2e3f73c?d=identicon&s=25 Rob Schultz (Guest)
on 2006-02-12 22:58
not sure if this is your problem or not but if you are using Rails 1.0
this might be your issue.
5224d56434a3d237408c217605df8a56?d=identicon&s=25 Payton Swick (Guest)
on 2006-02-13 02:16
(Received via mailing list)
"Fixed that saving a model with multiple habtm associations would only
save the first one."

Hm... the description doesn't sound like my problem, but I guess it
could definitely be a bug in rails.  Is it possible to get a newer
version of the gem?  Or do I need to use svn to get an updated version?

5224d56434a3d237408c217605df8a56?d=identicon&s=25 Payton Swick (Guest)
on 2006-02-13 02:28
(Received via mailing list)
This sounds exactly like my problem:

This is similar:

As is this:

Looks like a recurring problem for lots of people.  It looks like the
solution is to manually patch activerecord... but is there a better way?

Eac107645eb8b8198558af8af2e3f73c?d=identicon&s=25 Rob Schultz (Guest)
on 2006-02-13 02:34
i haven't done it yet but you can do this to get the the EdgeRails with
all the fixs. though not always an
option if you are wanting to deploy an app.
Af93ba6b6b59f22a8f37e8de5702ef98?d=identicon&s=25 Bob Silva (Guest)
on 2006-02-13 02:36
(Received via mailing list)
Have you tried edge rails?

Looks like a fix has been applied.

Bob Silva
5224d56434a3d237408c217605df8a56?d=identicon&s=25 Payton Swick (Guest)
on 2006-02-13 03:50
(Received via mailing list)

Revision 3332 makes it work!  Wow, I spent a whole day assuming I had
done something wrong there.  Thank you guys.

What's the release schedule (if any) for activerecord?  That is, any
idea when this patch will be in the gem?

This topic is locked and can not be replied to.