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
读取 AR属性
@payment_detail.send("#{address_type}_address_country")
Run Code Online (Sandbox Code Playgroud)
要么
@payment_detail.read_attribute("#{address_type}_address_country")
Run Code Online (Sandbox Code Playgroud)编写 AR属性
@payment_detail.send("#{address_type}_address_country=", value)
Run Code Online (Sandbox Code Playgroud)
要么
@payment_detail.write_attribute("#{address_type}_address_country", value)
Run Code Online (Sandbox Code Playgroud)设置实例变量
@payment_detail.instance_variable_set("@#{address_type}_address_country", value)
Run Code Online (Sandbox Code Playgroud)获取实例变量
@payment_detail.instance_variable_get("@#{address_type}_address_country")
Run Code Online (Sandbox Code Playgroud)参考
| 归档时间: |
|
| 查看次数: |
9279 次 |
| 最近记录: |