簡體   English   中英

Rails:如何以類似於alias_method_chain的方式來裝飾屬性?

[英]Rails: How to decorate an attribute in a similar fasion to alias_method_chain?

在rails中,這是一種常見的模式:

def foo_with_feature
  add_feature_to foo_without_feature
end

alias_method_chain :foo, :feature

我要執行此操作,但要使用一個屬性,該屬性的getter方法未定義,並且將通過method_missing即時完成,請參閱為什么alias_method在Rails模型中失敗

如何裝飾尚未設置的屬性獲取器?

只需直接訪問屬性即可,如下所示:

def fooattr_raw
  self.attributes["fooattr"]
end

def fooattr
  add_feature_to fooattr_raw
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM