Rails中的歧义条款4

时间:2016-01-20 04:45:14

标签: sql ruby-on-rails ruby ruby-on-rails-4

我加入了4个表

ListingBuyer.joins('left join buyers on listing_buyers.email = buyers.email left join buyer_infos on buyers.id = buyer_infos.buyer_id').joins(:listing).select('listing_buyers.*').where(["email LIKE ?",'%%'])

当我在sql上运行时。 errors =>

  

ActiveRecord :: StatementInvalid:Mysql2 :: Error:Column' email' in where子句不明确:SELECT listing_buyers。* FROM listing_buyers INNER JOIN listings ON listingsid = listing_buyers。{{1} list_buyers.email = buyers.email上的left join买家在buyer.id = buyer_infos.buyer_id WHERE(电子邮件LIKE' %%')

左边加入buyer_infos

之后,编辑sql命令

listing_id

上面的代码工作正常。

我想使用sql     .where(["电子邮件喜欢?",' %%']) 不想使用

ListingBuyer.joins('left join buyers on listing_buyers.email = buyers.email left join buyer_infos on buyers.id = buyer_infos.buyer_id').joins(:listing).select('listing_buyers.*').where(["listing_buyers.email LIKE ?",'%%'])

如何编辑此sql代码

where(["listing_buyers.email LIKE ?",'%%'])

可以使用

  ListingBuyer.joins('left join buyers on listing_buyers.email = buyers.email left join buyer_infos on buyers.id = buyer_infos.buyer_id').joins(:listing).select('listing_buyers.*')

请帮助我。

1 个答案:

答案 0 :(得分:0)

sql需要知道要与哪些表格电子邮件列进行比较 - 如果您希望将其与多个电子邮件列进行比较,那么您需要将它们组合在一起,如

where(["listings_buyer.email LIKE ? OR other_table.email LIKE ?",'%%','%%'])