[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

HABTM insertion deletes old entries

Preethi Sivakumar

1/30/2009 5:56:00 AM

Hi,

I've 2 models Profile & User and they have HABTM relationship.

---------------------
class Vcprofile < ActiveRecord::Base
has_and_belongs_to_many :users
end
----------------------
class Vcgbluser < ActiveRecord::Base
has_and_belongs_to_many :vcprofiles
end
----------------------


I've a screen designed in which i can assign one profile to so many
users(through list box).

Initially if i assign 2 users(say sam and rose) to the profile(say
profile1), an entry is made into the "profiles_users" mapping table.
-----------------------------------------------------------
@profile.users = @User (@user is a list of users selected)
------------------------------------------------------------

But later when i open the same page and select two more users(say jack
and jil) for the same profile(profile1) and submit, the old two
entries(sam and rose) are delted from the mapping table(profiles_uses)
and these two entries are added.

Why does HABTM delete the old entries(sam and rose) when a new
assignment is made?

Can anyone help?

Thanks in advance.
--
Posted via http://www.ruby-....

2 Answers

Scott Lillibridge

1/30/2009 1:06:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

In a HABTM relationship, assigning a new collection (using =) will delete
the old collection and set it to the new collection. If you want to add to a
collection, use the << operator.

On Thu, Jan 29, 2009 at 10:55 PM, Preethi Sivakumar <preethi1.s@tcs.com>wrote:

> Hi,
>
> I've 2 models Profile & User and they have HABTM relationship.
>
> ---------------------
> class Vcprofile < ActiveRecord::Base
> has_and_belongs_to_many :users
> end
> ----------------------
> class Vcgbluser < ActiveRecord::Base
> has_and_belongs_to_many :vcprofiles
> end
> ----------------------
>
>
> I've a screen designed in which i can assign one profile to so many
> users(through list box).
>
> Initially if i assign 2 users(say sam and rose) to the profile(say
> profile1), an entry is made into the "profiles_users" mapping table.
> -----------------------------------------------------------
> @profile.users = @User (@user is a list of users selected)
> ------------------------------------------------------------
>
> But later when i open the same page and select two more users(say jack
> and jil) for the same profile(profile1) and submit, the old two
> entries(sam and rose) are delted from the mapping table(profiles_uses)
> and these two entries are added.
>
> Why does HABTM delete the old entries(sam and rose) when a new
> assignment is made?
>
> Can anyone help?
>
> Thanks in advance.
> --
> Posted via http://www.ruby-....
>
>

Preethi Sivakumar

1/30/2009 1:22:00 PM

0

Scott Lillibridge wrote:
> In a HABTM relationship, assigning a new collection (using =) will
> delete
> the old collection and set it to the new collection. If you want to add
> to a
> collection, use the << operator.

It is working now.
Thank you so much Scott :)
--
Posted via http://www.ruby-....