모델에서 데이터를 추출할 때 쿼리에서의 not like를 구현하고 싶을때.


@perms = PaperTrail::Version.where(item_type: "Device").where.not(object_changes: ["%last_qc%"]).order('created_at DESC').limit(50)

- 이렇게 했을 경우에 쿼리는 다음과 같이 나오게 된다.

select  * from versions where item_type = "device" and ( object_changes != '%last_qc%') order by created_at DESC limit 50

이 것의 의미는 "%last_qc%"를 비교하는 쿼리가 된다. 즉 "" 안에 있는 %가 like가 아닌 % 문자열로 인식하게 된다.


PaperTrail::Version.where(item_type: "Device").where.not("object_changes LIKE?","%last_qc%").order('created_at DESC').limit(50)

> 컬럼명 안에 "LIKE?"를 넣어서 not이 아닌 not like로 인식하는 쿼리가 되게 한다.


select  * from versions where item_type = "device" and ( not(object_changes LIKE '%last_qc%')) order by created_at DESC limit 50



by 무위자연 2016. 2. 18. 17:24
| 1 |