Oop Rails:从模型返回大写字符串
在Rails 4中,我的模型中有一个属性。比如说Oop Rails:从模型返回大写字符串,oop,ruby-on-rails-4,model-view-controller,model,Oop,Ruby On Rails 4,Model View Controller,Model,在Rails 4中,我的模型中有一个属性。比如说Dog.name,我需要什么代码来始终返回Dog.name.capitalize,而不是数据库值 例如,在C#中,我将执行公共字符串名称{get{returnthis.capitalize};set;}。在Ruby中如何实现这一点?在您的模型中覆盖getter Class Dog < ActiveRecord::Base def title self[:title].capitalize end classdog
Dog.name
,我需要什么代码来始终返回Dog.name.capitalize
,而不是数据库值
例如,在C#中,我将执行
公共字符串名称{get{returnthis.capitalize};set;}
。在Ruby中如何实现这一点?在您的模型中覆盖getter
Class Dog < ActiveRecord::Base
def title
self[:title].capitalize
end
classdog
请参见阅读属性否决票,是否要解释并撤销否决票?