Ruby on rails Rails 5连接两个表上的where子句
如何在Ruby on rails Rails 5连接两个表上的where子句,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,如何在Reply表中包含where子句和以下查询 Reply.joins(:replier_account).where(replier_accounts: {account_type: 'reply' }) 我试着在结尾添加它,如下所示: Reply.joins(:replier_account).where(replier_accounts: {account_type: 'reply' }).where("created_at > ?", 7.days.ago) 但我收到了以下错
Reply
表中包含where子句和以下查询
Reply.joins(:replier_account).where(replier_accounts: {account_type: 'reply' })
我试着在结尾添加它,如下所示:
Reply.joins(:replier_account).where(replier_accounts: {account_type: 'reply' }).where("created_at > ?", 7.days.ago)
但我收到了以下错误:
ActiveRecord::StatementInvalid: PG::AmbiguousColumn: ERROR: column reference "created_at" is ambiguous
错误告诉您,它不知道要在哪个表上筛选“created_at”(据猜测,它们都已“created_at”)。尝试将表名附加到创建的位置;像这样:
Reply.joins(:replier_account)
.where(replier_accounts: {account_type: 'reply' })
.where("replier_accounts.created_at > ?", 7.days.ago)
错误告诉您,它不知道要在哪个表上筛选“created_at”(据猜测,它们都已“created_at”)。尝试将表名附加到创建的位置;像这样:
Reply.joins(:replier_account)
.where(replier_accounts: {account_type: 'reply' })
.where("replier_accounts.created_at > ?", 7.days.ago)
重写此查询的另一种方法是将WHERE条件合并到一个调用中 要获取其帐户在过去7天内创建的答复,请执行以下操作:
Reply.joins(:replier_account)
.where(
"replier_accounts.account_type = :type AND replier_accounts.created_at > :created_at",
{ type: 'reply', created_at: 7.days.ago }
)
Reply.joins(:replier_account)
.where(
"replier_accounts.account_type = :type AND replies.created_at > :created_at",
{ type: 'reply', created_at: 7.days.ago }
)
或者,获取在过去7天内创建的答复:
Reply.joins(:replier_account)
.where(
"replier_accounts.account_type = :type AND replier_accounts.created_at > :created_at",
{ type: 'reply', created_at: 7.days.ago }
)
Reply.joins(:replier_account)
.where(
"replier_accounts.account_type = :type AND replies.created_at > :created_at",
{ type: 'reply', created_at: 7.days.ago }
)
重写此查询的另一种方法是将WHERE条件合并到一个调用中 要获取其帐户在过去7天内创建的答复,请执行以下操作:
Reply.joins(:replier_account)
.where(
"replier_accounts.account_type = :type AND replier_accounts.created_at > :created_at",
{ type: 'reply', created_at: 7.days.ago }
)
Reply.joins(:replier_account)
.where(
"replier_accounts.account_type = :type AND replies.created_at > :created_at",
{ type: 'reply', created_at: 7.days.ago }
)
或者,获取在过去7天内创建的答复:
Reply.joins(:replier_account)
.where(
"replier_accounts.account_type = :type AND replier_accounts.created_at > :created_at",
{ type: 'reply', created_at: 7.days.ago }
)
Reply.joins(:replier_account)
.where(
"replier_accounts.account_type = :type AND replies.created_at > :created_at",
{ type: 'reply', created_at: 7.days.ago }
)