新手问题 scope 中 joins 的多表查询

ckl · 2014年03月27日 · 最后由 ckl 回复于 2014年03月27日 · 1911 次阅读
class Health::PhyExamRecordItem < ActiveRecord::Base
  belongs_to :phy_exam_record, :class_name=>"Health::PhyExamRecord"
  belongs_to :phy_exam_tpl_item, :class_name=>"Health::PhyExamTplItem"
  scope :get_record_items, -> (category_id,phy_exam_record_id) { 
    self.joins(:phy_exam_tpl_item).where("health_phy_exam_record_items.phy_exam_record_id = ?",phy_exam_record_id) & Health::PhyExamTplItem.group_category(category_id)
  }
end

说明:scope 方法中进行多表查询需要调用

输出结果不对!

self.joins(:phy_exam_tpl_item).where("health_phy_exam_record_items.phy_exam_record_id = ?",phy_exam_record_id).merge(Health::PhyExamTplItem.group_category(category_id))
  }

知道错误了!把&改成 merge 就可以了

需要 登录 后方可回复, 如果你还没有账号请 注册新账号