diff --git a/app/models/order_article.rb b/app/models/order_article.rb index 4b6a013b..743ead39 100644 --- a/app/models/order_article.rb +++ b/app/models/order_article.rb @@ -166,13 +166,7 @@ class OrderArticle < ApplicationRecord else # Creates a new article_price if neccessary # Set created_at timestamp to order ends, to make sure the current article price isn't changed - create_article_price!(price_attributes.merge(created_at: order.ends)) and save - # TODO: The price_attributes do not include an article_id so that - # the entry in the database will not "know" which article is - # referenced. Let us check the effect of that and change it or - # comment on the meaning. - # Possibly this is the real reason why the global price is not - # affected instead of the `created_at: order.ends` injection. + create_article_price!(price_attributes.merge(article_id: article_id, created_at: order.ends)) and save end # Updates ordergroup values diff --git a/db/migrate/20181201000305_ensure_article_for_article_price.rb b/db/migrate/20181201000305_ensure_article_for_article_price.rb new file mode 100644 index 00000000..8dc00274 --- /dev/null +++ b/db/migrate/20181201000305_ensure_article_for_article_price.rb @@ -0,0 +1,20 @@ +class EnsureArticleForArticlePrice < ActiveRecord::Migration + class ArticlePrice < ActiveRecord::Base; end + + def change + reversible do |dir| + dir.up do + execute <<-SQL + UPDATE article_prices SET article_id = ( + SELECT article_id FROM order_articles + WHERE article_price_id = article_prices.id + ) + WHERE article_id IS NULL + SQL + ArticlePrice.where(article_id: nil).destroy_all + end + end + + change_column_null :article_prices, :article_id, false + end +end diff --git a/db/schema.rb b/db/schema.rb index fea4e440..33e47f4b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -21,7 +21,7 @@ ActiveRecord::Schema.define(version: 20181205010000) do add_index "article_categories", ["name"], name: "index_article_categories_on_name", unique: true, using: :btree create_table "article_prices", force: :cascade do |t| - t.integer "article_id", limit: 4 + t.integer "article_id", limit: 4, null: false t.decimal "price", precision: 8, scale: 2, default: 0, null: false t.decimal "tax", precision: 8, scale: 2, default: 0, null: false t.decimal "deposit", precision: 8, scale: 2, default: 0, null: false