2014-03-26 4 views
2

으로 필드를 얻기 위해 어떻게 같은 이름을 가진 두 개의 테이블이 있고이 나는 바깥을 결합하고 모든 필드를 얻으려면레일 가입 - 같은 이름

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


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

에 참여하고있는 중이 야. 나는 다음과 같은 일을 할 때 의자에 앉을 수있는 테이블에서 이름과 이드 만 얻는다.

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
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 
+0

감사로 액세스 할 수 있습니다받을 수 있나요? – user3443720

+1

오름차순으로 명령을 수행하려면'.order ('name asc')' – user3443720