gpt4 book ai didi

ruby-on-rails - Rails Join - 如何获取同名字段

转载 作者:行者123 更新时间:2023-12-04 13:15:09 24 4
gpt4 key购买 nike

我有两个同名的表,正在执行连接

Visits::Appointment
name:string
id:integer
...


Places::Seatables
name:string
appointment_id:integer
id:integer
....

我想做一个外连接并获取所有字段。当我执行以下操作时,我只从可座位的 table 上获得名称和 ID。
  Visits::Appointment.joins("LEFT OUTER JOIN places_seatables ON  places_seatables.appointment_id=visits_appointments.id").where('checkout is null and "isActive" is true and noshow is false').select(visits_appointments.*,places_seatables.*')

我怎样才能得到两个字段而不是一个

最佳答案

appointments = Visits::Appointment
.joins("LEFT OUTER JOIN places_seatables ON places_seatables.appointment_id=visits_appointments.id")
.where('checkout is null and "isActive" is true and noshow is false')
.select(visits_appointments.*,places_seatables.name as seatable_name, places_seatables.id as seatable_id')

现在你可以访问它们
appointment = appointments.first
appointment.seatable_name
appointment.seatable_id

关于ruby-on-rails - Rails Join - 如何获取同名字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22671966/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com