【问题标题】:Rails Join - How to get fields with same nameRails Join - 如何获取具有相同名称的字段
【发布时间】:2014-05-05 12:03:54
【问题描述】:

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

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.*')

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

【问题讨论】:

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


    【解决方案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
    

    【讨论】:

    • 谢谢...您如何按名称订购?
    • 要按 asc 进行排序,只需附加 .order('name asc')
    • 我知道这是一个老答案,但是为什么在查询的每个部分都使用双引号、单引号和不使用引号?
    猜你喜欢
    • 2016-08-25
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    • 2011-11-20
    相关资源
    最近更新 更多