Sql 使用select从其他表中获取列值

Sql 使用select从其他表中获取列值,sql,ruby,postgresql,ruby-on-rails-4,Sql,Ruby,Postgresql,Ruby On Rails 4,我在我的交互器上有以下查询: Variant.where(code: context.codes) .joins("LEFT OUTER JOIN prices p ON p.variant_id = variants.id") .select("code, actual_price, selling_price, p.cost"

我在我的交互器上有以下查询:

 Variant.where(code: context.codes)
        .joins("LEFT OUTER JOIN prices p
                ON p.variant_id = variants.id")
        .select("code,
                 actual_price,
                 selling_price,
                 p.cost")
返回:

#<Interactor::Context codes=["PRDCTA-V1", "PRDCTA-V2"], body=[#<Variant id: nil, selling_price: nil, code: "PRDCTA-V2", actual_price: nil>,
#<Variant id: nil, selling_price: #<BigDecimal:7ff1d13b9118,'0.1E3',9(18)>, code: "PRDCTA-V1", actual_price: nil>]

尝试此操作,在“为成本分配别名”中选择

p.cost AS price_cost
然后,通过
variants获取它。首先,price\u cost

或者,只需从
attributes

variants.first.attributes['cost']
我将退还费用


希望有帮助

它没有出现,但无论如何都可以访问,请尝试
。首先。成本
@SebastianPalma编辑:它确实出现了!但是为什么呢?有没有一种方法可以将其包含在结果中?在内存中,但不会“推送”到变量对象。您可以将该列名作为方法进行迭代和调用。或者您需要一种特殊的方式来表示它们吗?您可以使用
.map(&:attributes)
将记录列表转换为普通散列,这些将具有键
成本
variants.first.attributes['cost']