Rails - 如何强制关联使用别名表名

时间:2013-12-23 05:48:44

标签: mysql ruby-on-rails activerecord

p = Patient.find(30)

p.patient_problems

以上代码生成以下查询

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

但有没有办法分配/使用别名table_name,如

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

因此它将生成以下查询

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)

其他信息

我的问题是当我尝试使用连接时

p.patient_problems(:所有,:加入=>联接)

我收到此错误

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) )  ))

当然,我可以对生成的连接查询进行一些字符串操作,并将别名设置为patient_problem。但我认为为关联设置别名会更清晰,因为生成的连接查询是不可预测的(在我的场景中)

3 个答案:

答案 0 :(得分:4)

我不确定变量joins是什么或者它是如何构造的。要在联接中对别名进行别名,请构建您的查询,如

Rails 3

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

PatientProblem.find(:all, :joins => "as p1 OUTER JOIN patient_problem as p2 ON ...")

答案 1 :(得分:0)

你可以为它制作单例方法并一次编写查询,并且可以像

一样使用
def self.p1
#your active record query here.
end 

并调用

PatientProblem.p1

答案 2 :(得分:-2)

<强>更新

您只需更改代码中的表名:

Patient.table_name="p2"

我不确定这是否会打破其他任何事情......祝你好运!

原始回答

一种解决方案可能是为每种类型的patient_problem定义一个单独的模型,然后执行以下操作:

class PatientProblem2 < ActiveRecord::Base
 self.set_table_name "p2"
 ...
end

另一种解决方案可能是使用ActiveRecord查询接口,这将允许显着的查询灵活性: http://guides.rubyonrails.org/active_record_querying.html

也许你可以更加具体地解决你想要解决的自然问题。