12
我有屬性「home_address_country」一個PaymentDetail模式,這樣我就可以使用如何在rails中使用變量作爲對象屬性?
@payment_detail.home_address_country //where @payment_detail is object of that model.
我想用這樣的:---
country_attribute=address_type+"_address_country" //where address type is equal to 'home'
@payment_detail."#{country_attribute}"
手段屬性名稱存儲在一個變量。我怎樣才能做到這一點?
EDIT
country_attribute=address_type+"_address_country"
country_list=Carmen::country_names
eval("@#{country_attribute} = #{country_list}")
僅供參考,我在Rails 4中測試了這個,並且#read_attribute仍然在運行 - 不確定#write_attribute。不過,我改用#send來代替。 – Dylan 2015-01-13 17:27:25