From bbe80b1409c97a6d2032818dde8c0cc517f9747a Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Mon, 15 Dec 2014 00:38:29 +0100 Subject: [PATCH] Create association of translations with their problems --- app/models/problem.rb | 1 + app/models/translation.rb | 2 ++ db/migrate/20141214221259_add_problem_to_translation.rb | 5 +++++ db/schema.rb | 5 ++++- 4 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20141214221259_add_problem_to_translation.rb diff --git a/app/models/problem.rb b/app/models/problem.rb index 4740c0b..ee70fe3 100644 --- a/app/models/problem.rb +++ b/app/models/problem.rb @@ -1,3 +1,4 @@ class Problem < ActiveRecord::Base has_one :translation + has_many :translations, inverse_of: :problem end diff --git a/app/models/translation.rb b/app/models/translation.rb index 628d87e..c598631 100644 --- a/app/models/translation.rb +++ b/app/models/translation.rb @@ -1,4 +1,6 @@ class Translation < ActiveRecord::Base + belongs_to :problem, inverse_of: :translations + validates :title, :content, presence: true validates :title, uniqueness: true diff --git a/db/migrate/20141214221259_add_problem_to_translation.rb b/db/migrate/20141214221259_add_problem_to_translation.rb new file mode 100644 index 0000000..c598504 --- /dev/null +++ b/db/migrate/20141214221259_add_problem_to_translation.rb @@ -0,0 +1,5 @@ +class AddProblemToTranslation < ActiveRecord::Migration + def change + add_reference :translations, :problem, index: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 4e5eff5..f0321ed 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20141214220056) do +ActiveRecord::Schema.define(version: 20141214221259) do create_table "problems", force: true do |t| t.datetime "created_at" @@ -26,6 +26,9 @@ ActiveRecord::Schema.define(version: 20141214220056) do t.text "content" t.datetime "created_at" t.datetime "updated_at" + t.integer "problem_id" end + add_index "translations", ["problem_id"], name: "index_translations_on_problem_id" + end