简体   繁体   English

将SQL查询转换为ActiveRecord查询导轨4

[英]convert sql query to Activerecord query rails 4

I have this sql query: 我有这个SQL查询:

      sub = "(select Name from other_holders s where s.share_holder_id=share_holders.id and s.other_holder_type_id=1) as secondHolderName"   
      sub1 = "(select Name from other_holders s where s.share_holder_id=share_holders.id and s.other_holder_type_id=2) as thirdHolderName"       
      @shareHolders=ShareHolder.select("Folio_no,Name,#{sub},#{sub1}")           

I am getting the required result from this query,But i am not satisfied with this.I want to convert this whole query to Activerecord query.Please help me out. 我从此查询中获得所需的结果,但是我对此不满意。我想将整个查询转换为Activerecord查询。请帮帮我。

class ShareHolder < ActiveRecord::Base
  has_many :second_holders, class_name: OtherHolder, ->{where( older_holder_type_id: 1)}
  has_many :third_holders, class_name: OtherHolder, ->{where(other_holder_type: 2)}
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM