From 0011a0b4c7ddc83dcbab239f16df43814c4b0ab2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= Date: Mon, 1 Apr 2019 11:59:11 +0200 Subject: [PATCH] Move Translation class inside Budget The reason to move Translation class is explained here [1] [1] https://github.com/consul/consul/pull/3359/commits/106649a8a598fb4b1389e174fdd6e70f51767af2 --- app/models/budget.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/models/budget.rb b/app/models/budget.rb index c4fe62eeb..4904f3923 100644 --- a/app/models/budget.rb +++ b/app/models/budget.rb @@ -229,4 +229,16 @@ class Budget < ApplicationRecord slug.nil? || drafting? end + class Translation < Globalize::ActiveRecord::Translation + validate :name_uniqueness_by_budget + + def name_uniqueness_by_budget + if Budget.joins(:translations) + .where(name: name) + .where.not("budget_translations.budget_id": budget_id).any? + errors.add(:name, I18n.t("errors.messages.taken")) + end + end + end + end