作者ihower (好2ˇ)
看板Ruby
標題Re: [問題] 相同的instance和class method
時間Mon May 10 17:21:58 2010
是指 instance method 跟 class method 一樣嗎? XD
def price
self.class.price
end
def self.price
...
end
※ 引述《bypang (甚麼時候才等到妳)》之銘言:
: 如果有相同結構的instance和class method,要如何做才能dry呢?
: class Product < ActiveRecord::Base
: def price
: ...
: end
: def self.price
: ...
: end
: end
: class ProductsController < ApplicationController
: # use in ajax call
: def calculate
: price = Product.price
: end
: end
: 謝謝各位大大
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.43.194.211
推 bypang:謝謝,真神奇,可以了 05/10 18:30
推 godfat:不建議這樣設計,很混淆 05/10 22:01
推 bypang:godfat大大有何建議嗎?一個是virtual attributes 05/11 12:42
推 bypang:一個是class method,可是都有相同的code 05/11 12:45
→ godfat:看起來不像是 virtual attribute,而是class method wrapper 05/11 15:17