summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
Diffstat (limited to 'app/models')
-rw-r--r--app/models/order.rb1
-rw-r--r--app/models/payment.rb7
2 files changed, 8 insertions, 0 deletions
diff --git a/app/models/order.rb b/app/models/order.rb
index ae5a0d5..213bde9 100644
--- a/app/models/order.rb
+++ b/app/models/order.rb
@@ -7,4 +7,5 @@ class Order < ApplicationRecord
has_one :payment
has_many :product_orders
has_many :products, through: :product_orders
+ accepts_nested_attributes_for :product_orders
end
diff --git a/app/models/payment.rb b/app/models/payment.rb
index 5d5e862..4839de8 100644
--- a/app/models/payment.rb
+++ b/app/models/payment.rb
@@ -5,4 +5,11 @@
class Payment < ApplicationRecord
has_one :order
belongs_to :card
+ validate :card_belongs_to_user
+
+ def card_belongs_to_user
+ return unless order.user_account.cards.find_by(id: card.id).nil?
+
+ errors.add(:card_id, "doesn't belong to user")
+ end
end