Fixed broken group_orders after editing order.
Caution, when using the model.association = models operator, the after save callbacks are not triggerd! See Order#save_order_articles
This commit is contained in:
parent
c18fb20115
commit
40d9195eac
2 changed files with 29 additions and 29 deletions
|
|
@ -214,14 +214,24 @@ class Order < ActiveRecord::Base
|
|||
end
|
||||
|
||||
def save_order_articles
|
||||
self.articles = Article.find(article_ids) # This doesn't deletes the group_order_articles, belonging to order_articles,
|
||||
# see http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many
|
||||
#self.articles = Article.find(article_ids) # This doesn't deletes the group_order_articles, belonging to order_articles,
|
||||
# # see http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many
|
||||
#
|
||||
## Ensure to delete also the group_order_articles, belonging to order_articles
|
||||
## This case is relevant, when removing articles from a running order
|
||||
#goa_ids = GroupOrderArticle.where(group_order_id: group_order_ids).includes(:order_article).
|
||||
# select { |goa| goa.order_article.nil? }.map(&:id)
|
||||
#GroupOrderArticle.delete_all(id: goa_ids) unless goa_ids.empty?
|
||||
|
||||
# Ensure to delete also the group_order_articles, belonging to order_articles
|
||||
# This case is relevant, when removing articles from a running order
|
||||
goa_ids = GroupOrderArticle.where(group_order_id: group_order_ids).includes(:order_article).
|
||||
select { |goa| goa.order_article.nil? }.map(&:id)
|
||||
GroupOrderArticle.delete_all(id: goa_ids) unless goa_ids.empty?
|
||||
|
||||
# fetch selected articles
|
||||
articles_list = Article.find(article_ids)
|
||||
# create new order_articles
|
||||
(articles_list - articles).each { |article| order_articles.create(:article => article) }
|
||||
# delete old order_articles
|
||||
articles.reject { |article| articles_list.include?(article) }.each do |article|
|
||||
order_articles.detect { |order_article| order_article.article_id == article.id }.destroy
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue