All in all I think you are over thinking this, how about
def find_record(rec)
if rec =~ /^[a-z]\d{2}-\d{2}-\d{4}$/i
return Student.where(:studentID => rec).all
elsif rec =~ /^[a-z]\d{8}$/i
new_rec = rec.insert(5,'-').insert(3,'-')
return Student.where(:studentID => new_rec).all
else
f_name, l_name = rec.split(/\s+/, 2)
return Student.where(:fname => f_name, :lname => l_name).all
end
end
The first match will match the pattern "a12-34-5678" and get the
records based on that as the studentID key.
The second match matches "a12345678" which is converts into the
correct format and does the same.
The final part assumes that if the input string does not match either
pattern then is *should* be a name in the format "john smith" (that is
to say a string with some whitespace in it) and will get all the
student records based on that.
To be honest you should check that last assumption incase someone
inputs "john", "dr john alan" or "john williams III".
Unit tests is your friend here :)
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.