我正在尝试从用户中删除相同的语言。
class User < ActiveRecord::Base
has_and_belongs_to_many :languages
end
class Language < ActiveRecord::Base
has_and_belongs_to_many :users, uniq: true
end
但是当我尝试做的时候
lang = Language.where(id: params[:user][:lang_1])
@user.languages << lang
logger.debug(@user.languages.include? Language.where(id: params[:user][:lang_1]))
include?
总是返回false,即使@user.languages
确实包含Language.where(id: params[:user][:lang_1])
。如何修复include?
方法?
答案 0 :(得分:0)
您需要保存@user
才能为用户存储新语言
lang = Language.where(id: params[:user][:lang_1])
@user.languages << lang
@user.save!
logger.debug(@user.languages.include?(lang))