2012-06-04 5 views
0

에있는 경우 확인이 내가하고 싶은 것입니다 : 대신에 temporaryCode. 하지만 어떻게해야할지 모르겠다. 미리 감사드립니다. 는 PARAMS 레일

+1

'경우 (PARAMS [: ID])'를 호출하여'id' 매개 변수를 검사합니다. – xdazz

+0

params id가 null이면 customercode에서 params [: id]를 사용하고 임시 코드로 params [: id] 대신 무엇을 사용 하시겠습니까? –

+0

@xdazz :'params.has_key? : id'는 그것이 당신이 의미하는 바에 따라 더 나아질 것입니다. –

답변

0

나는 데이터베이스 내부의 유착을 사용할 확신 :

customer = Customer.where(:siteId => params[:siteId]) 
        .where('coalesce(customercode, temporarycode) = ?', params[:id]) 
        .first 

는 SQL COALESCE 함수가 NULL이 자사의 인수의 첫 번째 반환합니다. 예를 들어 : column1가 null는 아니고, column2 경우 column1가 NULL 인 경우

coalesce(column1, column2) 

당신에게 column1을 제공합니다. 당신이 Rails2를 사용하는 경우


다음이 같은 작동합니다 : 측정기의

Customer.find(:first, :conditions => [ 
    'siteid = ? and coalesce(customercode, temporarycode) = ?', 
    params[:siteId], params[:id] 
]) 
+1

SQL 데이터베이스를 사용하는 경우에 따라 달라집니다. –

+0

@Sagiv : true하지만 그렇지 않은 경우를 제외하고는 매우 안전한 가정입니다. –

+0

.where는 레일즈 2에서 사용할 수 없습니다. 그래서 대신 다음을 시도했습니다 : –

1
customer = Customer.find_by_siteid_and_customercode params[:siteId], params[:id] 
customer ||= Customer.find_by_siteid_and_temporarycode params[:siteId], params[:id] 

하고 사용이 안전하고 가장이다, 청소기

관련 문제