I am trying to create a validation in a model (I think it would be better to validate it at a database level actually) so an user can not purchase and item that has already bought in a interval of 2 days (after 2 days it's ok if he/she buys it again).
I have a model for the "purchase" action, and every purchase action has an item associated called "purchasable". Also every purchase action has a user_id associated.
I am trying it like this in the purchase model (but my problem is that I don't know how to correctly write this validation, or even how to retrieve "purchasable.id" or the "user.id" when writing the method for the validation)
class Purchase < ApplicationRecord
belongs_to :purchasable, polymorphic: true
belongs_to :user
validates :price, presence: true
validate :no_purchase_overlap
def no_purchase_overlap
allPur = Purchase.includes(:purchasable).all
if allPur.where("created_at >= ? AND user_id = ? AND purchase_id = ?", (Time.now-2.days), user_id, purchasable.id)
errors.add(:date_end, 'it overlaps another purchase')
end
end
end
A purchase and its purchasable look like:
irb(main):006:0> Purchase.first
Purchase Load (0.1ms) SELECT "purchases".* FROM "purchases" ORDER BY "purchases"."id" ASC LIMIT ? [["LIMIT", 1]]
=> #<Purchase id: 1, price: 0.299e1, quality: "SD", purchasable_type: "Season", purchasable_id: 9, user_id: 1, created_at: "2020-02-25 09:34:59", updated_at: "2020-02-25 09:34:59">
irb(main):007:0> Purchase.first.purchasable
Purchase Load (0.1ms) SELECT "purchases".* FROM "purchases" ORDER BY "purchases"."id" ASC LIMIT ? [["LIMIT", 1]]
Season Load (0.2ms) SELECT "seasons".* FROM "seasons" WHERE "seasons"."id" = ? LIMIT ? [["id", 9], ["LIMIT", 1]]
=> #<Season id: 9, title: "Brandy of the Damned", plot: "Here's looking at you, kid.", number: 1, created_at: "2020-02-25 09:34:17", updated_at: "2020-02-25 09:34:17">
Right now it's failing my tests, it seems like it is not allowing any purchase to be saved:
it 'a movie can not be purchased by the same user more than once' do
purchase = Purchase.new(price: 2.99, quality: 'HD', purchasable: movie, user: user)
purchase_rep = Purchase.new(created_at: Time.now + 1.day ,price: 1.99, quality: 'SD', purchasable: movie, user: user)
expect { purchase.save }.to change { user.purchases.count }.by(1)
expect { purchase_rep.save }.to raise_error
end
failure message:
Failures:
1) Purchase When creating a purchase a movie can not be purchased by the same user more than once
Failure/Error: expect { purchase.save }.to change { user.purchases.count }.by(1)
expected `user.purchases.count` to have changed by 1, but was changed by 0
# ./spec/models/purchase_spec.rb:14:in `block (3 levels) in <main>'
Solved with
def no_purchase_overlap
if Purchase.where("created_at >= ? AND user_id = ?", (Time.now-2.days), self.user.id).any?{|purchase| purchase.purchasable == self.purchasable}
errors.add(:date_end, 'it overlaps another purchase')
end
end