Require active_record and refactor codes

Refactor for readability.

@makimoto please review this.