验证用户是否存在于 Active Record 的 2 个表中 (rails)

Validating if a user exists in 2 tables in Active Record (rails)

我有 2 个模型:提案和客户。 提案和客户有一个属性:“customer_id”和“updated_at”(rails 中的默认值)。

提案belongs_to:客户

我需要验证是否存在针对客户的提案以及该提案是否是昨天创建的。

我尝试了一些方法但没有成功。

我尝试使用代码:

Proposal.joins(:customer).where(customer: {customer_id: proposal: customer_id}, proposal.created_at = '2020-10-06 14:22:31'})

Proposal.joins("INNER JOIN customer ON Customer.customer_id = Proposal.customer_id AND Proposal.updated_at = '2020-10-06 14:22:31'")

Obs:我使用 2020-10-06 14:22:31 因为我的模型中有一些数据 update_at 是 ' 2020-10-06 14:22:31' 所以,我用它只是为了证明我的查询是否正确。

尝试了其他方法但没有成功。

我该如何查询?

没那么复杂。您可以这样查询:

date = Date.parse('2020-10-06')

Proposal.where(customer_id: customer_id, created_at: date.all_day)