mirror of
https://github.com/projekteuler/projekteuler.git
synced 2026-01-27 02:28:50 +01:00
Fix #14: Make uniqueness check among other problems
This commit is contained in:
@@ -2,7 +2,16 @@ class Translation < ActiveRecord::Base
|
||||
belongs_to :problem, inverse_of: :translations
|
||||
|
||||
validates :title, :content, :problem_id, presence: true
|
||||
validates :title, uniqueness: true
|
||||
validate :title_is_unique_among_other_problems
|
||||
|
||||
self.per_page = 50
|
||||
|
||||
def title_is_unique_among_other_problems
|
||||
Problem.where.not(id: problem_id).each do |problem|
|
||||
if problem.is_translated? and problem.title == title
|
||||
errors.add(:title, :taken)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user