Wednesday, February 01, 2006

find, find_by_name and ActiveRecord::RecordNotFound

Given a User with 'has_many' messages:

message = user.messages.find do {|m| m.subject == messageSubject}

errors out with RecordNotFound exception if no such record exists; while

message = user.messages.find_by_subject(messageSubject)

doesn't error out with an exception, but it returns a nil object, which can then be tested for nil.

0 Comments:

Post a Comment

<< Home