2012-01-22 2 views
0

핸드폰이있는 모델이 있습니다차단 find_by_attribute 메서드 활성 레코드

핸드폰은 데이터베이스의 특정 형식을 사용하여 저장됩니다. User.find_by_cellphone ("1234567890")을 가로 채고 휴대폰을 정규화 한 다음 "실제"find_by_cellphone ("123-456-7890")을 호출하고 싶습니다.

어떻게 생각하나요? find_by_cellphone의 이름을 ar_find_by_cellphone으로 바꾸고 find_by_cellphone을 덮어 쓰고 ar_find_by_cellphone을 호출하려고합니다. 다른 아이디어?

답변

1

실제로는 find_by_cellphone이라는 메서드가 없습니다.이 메서드는 마술처럼 Ruby의 method_missing을 동적으로 사용하는 ActiveRecord입니다.

당신이, 당신의 Cellphone 클래스의 클래스 메소드와 같은 find_by_cellphone를 정의하고 싶은 일을하려면 그것은 내장 한 대신이를 사용합니다 : 내 alias_method_chain가 작동하지 않는 이유

def self.find_by_cellphone(number) 
    normalized_number = ... # normalize your number 
    self.where(:cellphone => normalized_number).first 
end 
+0

빌어 먹을 맞 , 감사! – daniel

+0

find_by_cellphone => self.where (...)로 수정 하나 먼저 – daniel

+0

죄송합니다. 업데이트 됨. –