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.
334ae822566107a2ea2b7a7cb88bd1d6?d=identicon&s=25 Brutyn Nick (Guest)
on 2005-12-15 14: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?
A43430351a6d4fb6d61d000bd56ed19a?d=identicon&s=25 Francois Paul (Guest)
on 2005-12-15 14: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
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2005-12-15 14:57
(Received via mailing list)
On 12/15/05, Brutyn Nick <brutyn_nick@hotmail.com> 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)
334ae822566107a2ea2b7a7cb88bd1d6?d=identicon&s=25 Brutyn Nick (Guest)
on 2005-12-15 17:48
(Received via mailing list)
thanks, works like a charm ;)

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