如何在rails中使用变量作为对象属性?

Moh*_*ain 13 ruby ruby-on-rails

我有一个属性'home_address_country'的PaymentDetail模型,所以我可以使用

    @payment_detail.home_address_country //where @payment_detail is object of that model.
Run Code Online (Sandbox Code Playgroud)

我想用这样的东西:---

country_attribute=address_type+"_address_country"  //where address type is equal to 'home'
 @payment_detail."#{country_attribute}" 
Run Code Online (Sandbox Code Playgroud)

均值属性名称存储在变量中.我怎样才能做到这一点?

编辑

country_attribute=address_type+"_address_country"
country_list=Carmen::country_names
eval("@#{country_attribute} = #{country_list}")
Run Code Online (Sandbox Code Playgroud)

Har*_*tty 39

参考