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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Payton S. (Guest)
on 2006-02-12 21: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.new(params[:user])
     @user.groups = params[:user][:group_names].split(/\s+/).collect do
|group|
       Group.find(:first, :conditions => ["name = ?", group]) ||
Group.new({:name => group})
     end
     @user.save

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,
@attributes={"name"=>"eight"}>]

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
save.

-Payton
Rob S. (Guest)
on 2006-02-12 23:58
not sure if this is your problem or not but if you are using Rails 1.0
this might be your issue.
http://dev.rubyonrails.org/changeset/3332
Payton S. (Guest)
on 2006-02-13 03: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?

-Payton
Payton S. (Guest)
on 2006-02-13 03:28
(Received via mailing list)
This sounds exactly like my problem:

http://dev.rubyonrails.org/ticket/3692

This is similar:

http://dev.rubyonrails.org/ticket/3604

As is this:

http://dev.rubyonrails.org/ticket/3259

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?

-Payton
Rob S. (Guest)
on 2006-02-13 03:34
i haven't done it yet but you can do this to get the the EdgeRails with
all the fixs.
http://wiki.rubyonrails.com/rails/pages/EdgeRails though not always an
option if you are wanting to deploy an app.
Bob S. (Guest)
on 2006-02-13 03:36
(Received via mailing list)
Have you tried edge rails?

Looks like a fix has been applied.

http://dev.rubyonrails.org/ticket/3244


Bob S.
http://www.railtie.net/
Payton S. (Guest)
on 2006-02-13 04:50
(Received via mailing list)
Yes!

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?

-Payton
This topic is locked and can not be replied to.