views:

45

answers:

1

I have a PaymentDetail model with attribute 'home_address_country', so i can use

    @payment_detail.home_address_country //where @payment_detail is object of that model.

I want to use something like this:---

country_attribute=address_type+"_address_country"  //where address type is equal to 'home'
 @payment_detail."#{country_attribute}" 

Means attribute name is stored in a variable. How can i do this?

EDIT

country_attribute=address_type+"_address_country"
country_list=Carmen::country_names
eval("@#{country_attribute} = #{country_list}")
+2  A: 

Try this:

@payment_detail.send("#{address_type}_address_country")

Edit 1

To set an attribute:

@payment_detail.send("#{address_type}_address_country=", value)

To set an instance variable explicitly:

@payment_detail.instance_variable_set("@#{address_type}_address_country=", value)

Edit 2

instance_variable_set("@#{address_type}_address_country", Carmen::country_names)
KandadaBoggu
worked.. thanks... vote up and accepting in next 6 mins...
piemesons
and can u help in creating a dynamic instance variable like @home_address_country @billing_address_country
piemesons
Updated my answer, take a look.
KandadaBoggu
hey thnks for updating your answer but i want to create a normal dynamic which can be accessed in view file.
piemesons
edited my question. Please check it.
piemesons
Updated the answer again.
KandadaBoggu
Thank you a lot... :-)
piemesons