I have 2 models: User
and Purchase
.
(我有2个模型: User
和Purchase
。)
Purchases belong to Users. (购买属于用户。)
class User < ApplicationRecord
has_many :purchases
end
class Purchase < ApplicationRecord
belongs_to :user
enum status: %i[succeeded pending failed refunded]
end
In Rails 5.2, a validation error is raised when any modifications are made to a Purchase
with no associated User
.
(在Rails 5.2中,当对没有关联User
的Purchase
进行任何修改时,都会引发验证错误。)
This works great for new purchases, but it also throws errors when simply trying to save data on an existing purchase with a user that no longer exists in the database. (这对于新购买的产品非常有用,但是当仅尝试使用数据库中不再存在的用户来保存现有购买的数据时,也会引发错误。)
For example:
(例如:)
user = User.find(1)
# Fails because no user is passed
purchase = Purchase.create(status: 'succeeded')
# Succeeds
purchase = Purchase.create(user: user, status: 'succeeded')
purchase.status = 'failed'
purchase.save
user.destroy
# Fails because user doesn't exist
purchase.status = 'refunded'
purchase.save
I know I can prevent the second update from failing by making the association optional with belongs_to :user, optional: true
in the purchase model, but that cancels out the first validation for purchase creation as well.
(我知道我可以通过将购买模型中的belongs_to :user, optional: true
为可选,从而防止第二次更新失败,但这也取消了对购买创建的第一次验证。)
I could also just build my own custom validations for the user association, but I'm looking for a more conventional Rails way to do this.
(我也可以为用户关联建立自己的自定义验证,但是我正在寻找一种更常规的Rails方法来实现。)
ask by Doug translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…