Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - How to force associations to use alias table name

p = Patient.find(30)

p.patient_problems

The above code generates the following query

SELECT `patient_problem`.* FROM `patient_problem` WHERE `patient_problem`.`patient_id` = 30 AND (`patient_problem`.`record_status_id` = 1)

But is there any way to assign/use alias table_name like

p.patient_problems(:alias=>'p1') # just for Ex.. This code will not work
p.patient_problems(:alias=>'p2') # just for Ex.. This code will not work

So it will generate the following queries

SELECT `p1`.* FROM `patient_problem` AS `p1` WHERE `p1`.`patient_id` = 30 AND (`p1`.`record_status_id` = 1)

SELECT `p2`.* FROM `patient_problem` AS `p2` WHERE `p2`.`patient_id` = 30 AND (`p2`.`record_status_id` = 1)

Additional Info

My problem is when I try to use joins

p.patient_problems(:all,:joins=>joins)

I get this error

ActionView::Template::Error (Mysql2::Error: Not unique table/alias: 'patient_problem': SELECT `patient_problem`.* FROM `patient_problem` LEFT OUTER JOIN party on party.id = patient_problem.patient_id 
        LEFT OUTER JOIN party_identifier on party.id = party_identifier.party_id
        LEFT OUTER JOIN blood_type on blood_type.id = party.blood_type_id
        LEFT OUTER JOIN education_level on education_level.id = party.education_level_id
        LEFT OUTER JOIN religion on religion.id = party.religion_id
        LEFT OUTER JOIN living_arrangement on living_arrangement.id = party.living_arrangement_id
      LEFT OUTER JOIN patient_problem patient_problem on patient_problem.patient_id = party.id and patient_problem.record_status_id = 1 
          left join (select user_type,username,user_id,auditable_id from (select MAX(id) id from audits where audits.auditable_type = 'PatientProblem' and user_type is not null group by auditable_id ) t inner join audits v on v.id=t.id ) entered_by1 on entered_by1.auditable_id = patient_problem.id
          left outer join user user1 on entered_by1.user_id = user1.id
          left outer join party as party_user1 on party_user1.id = user1.person_id
         LEFT OUTER JOIN patient_patient_search patient_patient_search1  on patient_patient_search1.patient_id = party.id
         left join search search1 on patient_patient_search1.patient_search_id = search1.id
         and patient_patient_search1.patient_search_id = '75' WHERE `patient_problem`.`patient_id` = 45 AND (`patient_problem`.`record_status_id` = 1) AND (  (patient_problem.occurrence_date > '2013-01-01 00:00:00' and patient_problem.occurrence_date < '2013-06-30 23:59:59' and   patient_problem.patient_problem_status_id in (5) and  patient_problem.code is not null and  patient_problem.code in ('10725009') )  and   (  patient_patient_search1.patient_search_id in (75.0) )  ))

Ofcourse I could do some string manipulation on the generated joins query and set alias to patient_problem. But I thought setting alias for associations would be more cleaner since the joins query generated are unpredictable(in my scenario)

like image 905
Siva Avatar asked Dec 23 '13 05:12

Siva


1 Answers

I am not sure what the variable joins is or how it was constructed. To alias tables in a join build your query like

Rails 3

PatientProblem.joins("as p1 OUTER JOIN patient_problem as p2 on ...")

or

PatientProblem.find(:all, :joins => "as p1 OUTER JOIN patient_problem as p2 ON ...")
like image 90
Sully Avatar answered Nov 03 '22 07:11

Sully