开发者

Rails attributes overriding

开发者 https://www.devze.com 2023-02-26 12:10 出处:网络
I have a Model with an att开发者_C百科riubte \"name\". I would like to be able to create a getter \"name\" that returns name.capitalize.

I have a Model with an att开发者_C百科riubte "name". I would like to be able to create a getter "name" that returns name.capitalize.

The problem, however, is that when the model is saved, the capitalized name is being written to the database. Suggestions?


def name
  attributes["name"].capitalize
end

and better to use another name for method

def capitalized_name # or "cap_name"
  name.capitalize
end


def name
  read_attribute(:name).capitalize
end
0

精彩评论

暂无评论...
验证码 换一张
取 消