How to delete a record

hey,
i my database i have users and groups, each user can get in different
groups

my db structure:
table groups: id, name, basegroup, firm_id
table users: id, firstname, lastname, email
table groups_users: group_id, user_id

my relation is a many to many:
class Group < ActiveRecord::Base
has_and_belongs_to_many :users
end
class User < ActiveRecord::Base
has_and_belongs_to_many :groups
end

i want this
http://wiki.script.aculo.us/scriptaculous/show/SortableListsDemo
with ajax, first column the user who arent in the group, the second
column those
who are in the group. (is cooler than 2 oldschool selectlists :wink: )

now i want to delete this record ex: (i want to remove the link between
the user
and the group, but wanna keem the user and group object
group_id = 5
users_id = 10

User.connection.delete("DELETE * FROM groups_users WHERE group_id = " +
group_id

  • " and user_id = " + users_id)

Anyone can help me?

collection.delete(object, …) - removes one or more objects from the
collection by removing their associations from the join table. This does
not destroy the objects

thanks, works like a charm :wink:

Nick

On 12/15/05, Brutyn N. [email protected] wrote:

has_and_belongs_to_many :users

and the group, but wanna keem the user and group object
group_id = 5
users_id = 10

User.connection.delete("DELETE * FROM groups_users WHERE group_id = " + group_id

  • " and user_id = " + users_id)

Anyone can help me?

From the API Docs:
http://rails.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000474

collection.delete(object, â?¦) - removes one or more objects from the
collection by removing their associations from the join table. This
does not destroy the objects.

@users.groups.delete(@group)

or

@groups.users.delete(@user)