字段多状态

Before

classPhoto<ActiveRecord::Base 
    STATUSES =['queued','new','changed','removed','ready']
def change_status self.status ='changed'

end

end
 

then

classPhoto<ActiveRecord::Base 
    STATUSES ={queued:'queued',new:'new', changed:'changed', removed:'removed', ready:'ready'}

    validates :status, inclusion:{in: STATUSES.values}

def change_status self.status = STATUSES[:new]

end

end
 

now

classPhoto<ActiveRecord::Base 
    STATUSES =[STATUS_QUEUED ='queued', STATUS_NEW ='new', STATUS_CHANGED ='changed', STATUS_REMOVED ='removed', STATUS_READY ='ready']

    validates :status, inclusion:{in: STATUSES}

def change_status self.status =Photo::STATUS_QUEUED end

end
原文地址:https://www.cnblogs.com/qinyan20/p/3643224.html