新手问题 mongoid has_and_belongs_to_many 疑惑

itsvoid · 2012年11月13日 · 2570 次阅读

这个问题纠结了我很长时间了,求解。我的上下文是这样的: 我有2个model,一个是User,另外一个是CustomSearchEngine,两者关系为has_and_belongs_to_many,具体定义如下:

class User
  include Mongoid::Document
  # Include default devise modules. Others available are:
  ......
  # keep the CSEs
  has_and_belongs_to_many :keeped_custom_search_engines, class_name: 'CustomSearchEngine', inverse_of: :consumers

  # create or fork the CSEs
  has_many :custom_search_engines, inverse_of: :author, dependent: :destroy

  # Index
  index({username: 1}, {unique: true, name: 'user_username'})
  index({email: 1}, {unique: true, name: 'user_email'})

  # Massive assignment for User.new
  attr_accessible :email, :username, :agreement, :password, :password_confirmation
  attr_accessor :agreement, :password_confirmation
  validates :password_confirmation, presence: true
  validates :agreement, presence: true
end

class CustomSearchEngine
  include Mongoid::Document
  include Mongoid::Timestamps
  paginates_per 20
 ...
  belongs_to :author, class_name: 'User', inverse_of: :custom_search_engines
  has_and_belongs_to_many :consumers, class_name: 'User', inverse_of: :keeped_custom_search_engines
  belongs_to :node

  # Index
  index({author_id: 1}, {name: 'cse_author_id'})
  index({node_id: 1}, {name: 'cse_node_id'})

  # validations
  validates :status, presence: true, inclusion: {in: ['draft', 'publish']}
  validates :author_id, presence: true
  validates :node_id, presence: true

  scope :recent, ->(status) { where(status: status).desc(:created_at) }
  ...
end

然后current_user.keeped_custom_search_engines.push custom_search_engine, 数据库user的keeped_custom_search_engine_ids成功push,但是custom_search_engine的consumer_ids确为空,查询错误记录,有一条是: @messages={:consumers=>["是无效的"]}

这是什么问题呢???

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册