【问题标题】:How to alias a table name?如何给表名起别名?
【发布时间】:2020-07-09 18:33:11
【问题描述】:

自从升级到 Rails 5 后,我的查询不起作用。它无法通过别名从表中获取结果。错误是“from”现在接受零参数。 arel的版本是9.0,Rails的版本是5.2.4.3

      offers = Offer.arel_table
      o2 = offers.alias("o2")

      seen_offers = offers.from("offers as o2").project(o2[:merchant_id], o2[:display_name])

这是错误:

ArgumentError: wrong number of arguments (given 1, expected 0) 

【问题讨论】:

    标签: ruby-on-rails activerecord arel


    【解决方案1】:

    Arel::Table#from 接受 0 个参数,因为它生成 SelectManager

    SelectManager.new(self)
    

    您正在寻找的是SelectManager#from,并且该问题很容易纠正(只需切换projectfrom

    offers.project(o2[:merchant_id], o2[:display_name]).from(o2)
    

    project 将返回 SelectManager,然后 from 将简单地更改源

    【讨论】:

      猜你喜欢
      • 2022-01-22
      • 2012-10-07
      • 1970-01-01
      • 2013-04-19
      • 2015-02-16
      • 2011-02-02
      • 2010-12-13
      • 1970-01-01
      相关资源
      最近更新 更多