Forum: Ruby on Rails How to delete a record

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.
Brutyn N. (Guest)
on 2005-12-15 15:36
(Received via mailing list)
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/Sor...
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?
Francois P. (Guest)
on 2005-12-15 15:54
(Received via mailing list)
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
Rick O. (Guest)
on 2005-12-15 15:57
(Received via mailing list)
On 12/15/05, Brutyn N. <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/...

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)
Brutyn N. (Guest)
on 2005-12-15 18:48
(Received via mailing list)
thanks, works like a charm ;)

Nick
This topic is locked and can not be replied to.