Ruby on rails 3 MongoID多对多关系

Ruby on rails 3 MongoID多对多关系,ruby-on-rails-3,mongodb,mongoid,mongoid3,Ruby On Rails 3,Mongodb,Mongoid,Mongoid3,我有两个模型,它们有以下关系: Class User has_and_belongs_to_many :notification_channels Class NotificationChannel has_and_belongs_to_many :users 我能够以这种方式为用户对象添加通知通道 @user.notification_channels << @notification 如何从用户频道中删除频道?以下是解决方案 @user.notification_chan

我有两个模型,它们有以下关系:

Class User
has_and_belongs_to_many :notification_channels

Class NotificationChannel
has_and_belongs_to_many :users
我能够以这种方式为用户对象添加通知通道

@user.notification_channels << @notification
如何从用户频道中删除频道?

以下是解决方案

 @user.notification_channels -= [NotificationChannel.find_by(id: params[:channel_id])] # relationship is removed both ways and both objects are saved

我会成功的

 @user.notification_channels -= [NotificationChannel.find_by(id: params[:channel_id])] # relationship is removed both ways and both objects are saved
 @user.notification_channels.delete(NotificationChannel.find_by(id: params[:channel_id])) # relationship is removed both ways but @user needs to be saved manually
 @user.save