1
我在這個問題上堅持了幾天..請幫助,提前致謝。mongoid不能用鐵軌推HABTM關係
這是mongoid Rails項目,也就是2個車型項目,一個是用戶,另一個CustomSearchEngine:
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
在我CustomSearchEngine控制器:
current_user.keeped_custom_search_engines.push(@custom_search_engine)
那我走了到我的mongodb,我只看到用戶文件更新了:
keeped_custom_search_engine_ids: ["50a208092061c770190000df"]
但是自定義搜索引擎的文檔沒有改變:
consumer_ids: []
我得到一個錯誤:@messages={:consumers=>["is invalid"]}
東西我錯過了什麼?
嘗試採取索引斷開CustomSearchEngine模型,看看它是否依然存在呢。當我遇到這樣的問題是因爲我聲明瞭相互矛盾的索引。不知道這是否會解決它,但很容易嘗試 – SporkInventor
我試過你的建議,但它沒有奏效。這個錯誤對我來說意味着驗證。但我沒有發現任何錯誤的驗證。 – firsthym