class Track < ActiveRecord::Base # # All the usual stuff # before_save :update_mp3_info # # More stuff # protected def update_mp3_info if mp3? if mp3.dirty? Mp3Info.open(mp3.to_file.path) do |mp3info| self.length = mp3info.length %w(title artist album year genre).each { |attr| send("#{attr}=", mp3info.tag.send(attr)) } end end else self.length = 0 %w(title artist album year genre).each { |attr| send("#{attr}=", '') } end true rescue Mp3InfoError => e errors.add(:mp3, "unable to process file (#{e.message})") false end end