# How to delete a record

**URL:** <https://www.ruby-forum.com/t/how-to-delete-a-record/66067>\
**Category:** Rails\
**Created:** [December 15, 2005, 2:36pm UTC](https://www.ruby-forum.com/t/how-to-delete-a-record/66067 "2005-12-15T14:36:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brutyn\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5f8ce5/32.png) [@Brutyn\_N](https://www.ruby-forum.com/u/Brutyn_N)\
**Post date:** [December 15, 2005, 2:36pm UTC](https://www.ruby-forum.com/t/how-to-delete-a-record/66067/1 "2005-12-15T14:36:23Z")

</div>

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](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 😉 )

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?

---

<div class="post-metadata">

**Author:** ![Brutyn\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5f8ce5/32.png) [@Brutyn\_N](https://www.ruby-forum.com/u/Brutyn_N)\
**Post date:** [December 15, 2005, 2:54pm UTC](https://www.ruby-forum.com/t/how-to-delete-a-record/66067/2 "2005-12-15T14:54:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Brutyn\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5f8ce5/32.png) [@Brutyn\_N](https://www.ruby-forum.com/u/Brutyn_N)\
**Post date:** [December 15, 2005, 5:48pm UTC](https://www.ruby-forum.com/t/how-to-delete-a-record/66067/3 "2005-12-15T17:48:38Z")

</div>

thanks, works like a charm 😉

Nick

---

<div class="post-metadata">

**Author:** ![Brutyn\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5f8ce5/32.png) [@Brutyn\_N](https://www.ruby-forum.com/u/Brutyn_N)\
**Post date:** [December 15, 2005, 2:57pm UTC](https://www.ruby-forum.com/t/how-to-delete-a-record/66067/4 "2005-12-15T14:57:32Z")

</div>

On 12/15/05, Brutyn N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](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)
