DonutWorks / Ari

0 stars 0 forks source link

user.valid?를 호출했을 경우 phone_number값이 바뀌지 않도록 하기 #332

Open shaynekang opened 9 years ago

shaynekang commented 9 years ago

사용자(User) 모델의 휴대폰 번호(phone_number) 컬럼에 "010-1234-5678"을 넣고 valid?를 호출하면 휴대폰 번호가 "01012345678"로 Normalize 되네요.

irb(main):113:0> user = User.new
=> #<User id: nil, email: nil, created_at: nil, updated_at: nil, username: nil, phone_number: nil, major: nil, student_id: nil, sex: nil, home_phone_number: nil, emergency_phone_number: nil, habitat_id: nil, member_type: nil, generation_id: nil, birth: nil>
irb(main):114:0> user.phone_number = "010-1234-5678"
=> "010-1234-5678"
irb(main):115:0> user.valid?
  User Exists (0.3ms)  SELECT  1 AS one FROM "users"  WHERE "users"."phone_number" = '01012345678' LIMIT 1
  User Exists (0.2ms)  SELECT  1 AS one FROM "users"  WHERE "users"."email" IS NULL LIMIT 1
=> false
irb(main):116:0> user
=> #<User id: nil, email: nil, created_at: nil, updated_at: nil, username: nil, phone_number: "01012345678", major: nil, student_id: nil, sex: nil, home_phone_number: nil, emergency_phone_number: nil, habitat_id: nil, member_type: nil, generation_id: nil, birth: nil>

사실 좀 미묘한 이슈라 고민을 했는데, 그래도 valid?는 데이터 검증만 하는 메소드인데 값이 바뀌는 건 올바르지 않은 것 같습니다. 수정이 된다면 좋겠네요. ㅎㅎ

shaynekang commented 9 years ago

아니면 차라리 phone_number를 넣을 때부터 자동으로 normalize 되는 것도 좋을 것 같습니다. ㅎㅎ

minhyeok4dev commented 9 years ago

저도 유사한 이슈를 겪었습니다.