0
什麼是使用Mongoid和Rails限制嵌入文檔數量的正確方法。限制Mongoid中嵌入文檔的數量
我已經試過這樣:
class League
include Mongoid::Document
embeds_many :teams
validate :validate_teams
def validate_teams
if teams.size > 6
errors.add(:base, "too many teams")
end
if !errors.empty?
raise Mongoid::Errors::Validations.new(self)
end
end
end
但是,這將打破它:
# Get a league with 5 teams.
league = League.first
# Get a copy of the league.
copy = League.first
# Create a new team for the first instance of the league and save.
league.teams.build
league.save
# Create a new team for the second instance and save.
copy.teams.build
copy.save
league.reload.teams.size # => 7
這種情況下可以在生產中使用的Rails應用程序運行多個實例變得明顯和響應請求併發。我需要一個堅如磐石的方法來限制嵌入文檔的數量。什麼是正確的方法來做到這一點?
你有一個要點,或與您optimistic_locking版本回購? – transGLUKator