# Habtm doesn't save old IDs

**URL:** <https://www.ruby-forum.com/t/habtm-doesnt-save-old-ids/66197>\
**Category:** Rails\
**Created:** [February 12, 2006, 8:23pm UTC](https://www.ruby-forum.com/t/habtm-doesnt-save-old-ids/66197 "2006-02-12T20:23:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Payton\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/74df32/32.png) [@Payton\_S](https://www.ruby-forum.com/u/Payton_S)\
**Post date:** [February 12, 2006, 8:23pm UTC](https://www.ruby-forum.com/t/habtm-doesnt-save-old-ids/66197/1 "2006-02-12T20:23:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Payton\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/74df32/32.png) [@Payton\_S](https://www.ruby-forum.com/u/Payton_S)\
**Post date:** [February 12, 2006, 10:58pm UTC](https://www.ruby-forum.com/t/habtm-doesnt-save-old-ids/66197/2 "2006-02-12T22:58:05Z")

</div>

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](http://dev.rubyonrails.org/changeset/3332)

---

<div class="post-metadata">

**Author:** ![Payton\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/74df32/32.png) [@Payton\_S](https://www.ruby-forum.com/u/Payton_S)\
**Post date:** [February 13, 2006, 2:16am UTC](https://www.ruby-forum.com/t/habtm-doesnt-save-old-ids/66197/3 "2006-02-13T02:16:21Z")

</div>

“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

---

<div class="post-metadata">

**Author:** ![Payton\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/74df32/32.png) [@Payton\_S](https://www.ruby-forum.com/u/Payton_S)\
**Post date:** [February 13, 2006, 2:28am UTC](https://www.ruby-forum.com/t/habtm-doesnt-save-old-ids/66197/4 "2006-02-13T02:28:49Z")

</div>

This sounds exactly like my problem:

[http://dev.rubyonrails.org/ticket/3692](http://dev.rubyonrails.org/ticket/3692)

This is similar:

[http://dev.rubyonrails.org/ticket/3604](http://dev.rubyonrails.org/ticket/3604)

As is this:

[http://dev.rubyonrails.org/ticket/3259](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

---

<div class="post-metadata">

**Author:** ![Payton\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/74df32/32.png) [@Payton\_S](https://www.ruby-forum.com/u/Payton_S)\
**Post date:** [February 13, 2006, 2:34am UTC](https://www.ruby-forum.com/t/habtm-doesnt-save-old-ids/66197/5 "2006-02-13T02:34:21Z")

</div>

i haven’t done it yet but you can do this to get the the EdgeRails with  
all the fixs.  
[Peak Obsession](http://wiki.rubyonrails.com/rails/pages/EdgeRails) though not always an  
option if you are wanting to deploy an app.

---

<div class="post-metadata">

**Author:** ![Payton\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/74df32/32.png) [@Payton\_S](https://www.ruby-forum.com/u/Payton_S)\
**Post date:** [February 13, 2006, 3:50am UTC](https://www.ruby-forum.com/t/habtm-doesnt-save-old-ids/66197/6 "2006-02-13T03:50:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Payton\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/74df32/32.png) [@Payton\_S](https://www.ruby-forum.com/u/Payton_S)\
**Post date:** [February 13, 2006, 2:36am UTC](https://www.ruby-forum.com/t/habtm-doesnt-save-old-ids/66197/7 "2006-02-13T02:36:10Z")

</div>

Have you tried edge rails?

Looks like a fix has been applied.

[http://dev.rubyonrails.org/ticket/3244](http://dev.rubyonrails.org/ticket/3244)

Bob S.  
[http://www.railtie.net/](http://www.railtie.net/)
