ruby-on-rails - Rails 5 joins where clause on both tables
问题描述
How can I include a where clause on the Reply
table with the following query?
Reply.joins(:replier_account).where(replier_accounts: {account_type: 'reply' })
I tried adding it at the end like so:
Reply.joins(:replier_account).where(replier_accounts: {account_type: 'reply' }).where("created_at > ?", 7.days.ago)
But I'm receiving the following error:
ActiveRecord::StatementInvalid: PG::AmbiguousColumn: ERROR: column reference "created_at" is ambiguous
解决方案
该错误告诉您它不知道您希望在哪个表上过滤“created_at”(猜测它们都有“created_at”)。尝试将表名附加到 created_at; 像这样:
Reply.joins(:replier_account)
.where(replier_accounts: {account_type: 'reply' })
.where("replier_accounts.created_at > ?", 7.days.ago)
推荐阅读
- autodesk-forge - 大型 IFC 文件的 Forge 模型衍生失败
- android - 邮递员工作正常,但 Android 应用程序没有收到通知
- javascript - angularjs 更新按钮文本基于 $http 响应 ng-click
- python-3.x - ctcsound 不是有效的 Win32 应用程序
- c - Linux系统调用何时触发段错误与返回EFAULT?
- regex - 在记事本++中用正则表达式查找和替换可变数字
- php - 条件如果包含一个 php 文件
- python - Heroku:在 django 中迁移时出现“django.db.utils.DataError: value too long for type character varying(20)”错误
- python - Instax-api 问题 --- 仅打印小 (400 x 400) jpg 而非全分辨率 600 x 800 图像
- unity3d - 如果自上次动画以来没有足够的时间,Unity Animation 将不会播放