summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
Diffstat (limited to 'app/models')
-rw-r--r--app/models/order.rb8
-rw-r--r--app/models/payment.rb2
-rw-r--r--app/models/user_account.rb1
3 files changed, 8 insertions, 3 deletions
diff --git a/app/models/order.rb b/app/models/order.rb
index 213bde9..519de09 100644
--- a/app/models/order.rb
+++ b/app/models/order.rb
@@ -4,8 +4,12 @@
# public_id: string
class Order < ApplicationRecord
belongs_to :user_account
- has_one :payment
- has_many :product_orders
+ has_one :payment, dependent: :destroy
+ has_many :product_orders, dependent: :destroy
has_many :products, through: :product_orders
accepts_nested_attributes_for :product_orders
+
+ def to_param
+ public_id
+ end
end
diff --git a/app/models/payment.rb b/app/models/payment.rb
index 4839de8..8be9c7f 100644
--- a/app/models/payment.rb
+++ b/app/models/payment.rb
@@ -8,7 +8,7 @@ class Payment < ApplicationRecord
validate :card_belongs_to_user
def card_belongs_to_user
- return unless order.user_account.cards.find_by(id: card.id).nil?
+ return if Order.find_by(id: order_id)&.user_account&.cards&.exists?(card_id)
errors.add(:card_id, "doesn't belong to user")
end
diff --git a/app/models/user_account.rb b/app/models/user_account.rb
index 3e05056..6740f32 100644
--- a/app/models/user_account.rb
+++ b/app/models/user_account.rb
@@ -13,6 +13,7 @@ class UserAccount < ApplicationRecord
has_many :addresses, through: :user_account_addresses
has_many :cards, dependent: :destroy
has_many :product_reviews
+ has_many :orders
validates :email, presence: true
validates :email, uniqueness: true