2013-01-18 107 views
2

我有兩個型號,下面的關係,MongoID多對多關係船

Class User 
has_and_belongs_to_many :notification_channels 

Class NotificationChannel 
has_and_belongs_to_many :users 

我能夠添加通知通道用戶對象以這種方式

@user.notification_channels << @notification 

但是除去用戶的通道頻道將刪除渠道文件從渠道收集與以下查詢

@user.notification_channels.find_by(id: params[:channel_id]).destroy 

我如何從用戶頻道中刪除頻道?

回答

1

下面是解

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

OR

@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 

會做的伎倆。