Rails Join - 如何获取具有相同名称的字段

时间:2014-03-26 20:08:41

标签: ruby-on-rails ruby-on-rails-3 join outer-join

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

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


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

我想做一个外连接并获取所有字段。当我执行以下操作时,我只从座位表中获取名称和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.*')

如何获得两个字段而不是一个

1 个答案:

答案 0 :(得分:1)

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