[英]How to loop through all attributes and alias_attribute on all of them?
我有一個與舊數據庫通信的Rails應用程序,其中所有列都以大寫TYPE
,例如NAME
, TYPE
, CREATED_AT
。 不能將這些列重命名為小寫。
我知道可以使用alias_attribute, :new_name, :old_name
創建別名。
但是,是否有一種方法可以遍歷所有列並為這些屬性別名以小寫形式? 類似於以下內容:
class Apple < ActiveRecord::Base
Apple.attribtues.each do |att|
alias_attribute :att.down_case, :att
end
end
attributes
是一個實例方法,而column_names
是一個適合您目的的類方法。
class Apple < ActiveRecord::Base
Apple.column_names.each do |att|
alias_attribute :att.downcase, :att
end
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.