Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a variable as object attribute in rails?

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}")
like image 426
Mohit Jain Avatar asked Oct 12 '10 10:10

Mohit Jain


People also ask

Is a variable an object in Ruby?

A variable itself is not an object. "A variable in Ruby is just a label for a container. A variable could contain almost anything - a string, an array, a hash. A variable name may only contain lowercase letters, numbers, and underscores.

How to access class variable in Ruby?

Re accessing instance/class variables (mainly from the console), for me it worked like FooClass. class_variable_set(:@@foo, 'bar') and FooClass. class_variable_get(:@@foo) , and for instances foo_class. instance_variable_set(:@foo, 'baz') and foo_class.

How do you define a variable in Rails?

The short answer, if you're in the controller and you need to make the variable available to the view then use @variable . Show activity on this post. Variables without the @ symbol are called local variables, which means you can access these local variables within THAT DECLARED METHOD only. Limited to the local scope.

Can a class method access instance variables Ruby?

class . There are no "static methods" in the C# sense in Ruby because every method is defined on (or inherited into) some instance and invoked on some instance. Accordingly, they can access whatever instance variables happen to be available on the callee.


2 Answers

  • Reading AR attribute

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

    OR

    @payment_detail.read_attribute("#{address_type}_address_country")
    
  • Writing AR attribute

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

    OR

    @payment_detail.write_attribute("#{address_type}_address_country", value)
    
  • Setting instance variable

    @payment_detail.instance_variable_set("@#{address_type}_address_country", value)
    
  • Getting instance variable

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

Reference

  • send method documentation
  • read_attribute method documentation
  • write_attribute method documentation
  • instance_variable_get method documentation
  • instance_variable_set method documentation
like image 119
Harish Shetty Avatar answered Oct 26 '22 11:10

Harish Shetty


Recommended way for Rails 3 is to use dictionary-like access:

attr = @payment_detail["#{address_type}_address_country"]
attr = "new value"
@payment_detail["#{address_type}_address_country"] = attr

read_attribute and write_attribute methods work for Rails 2 only.

like image 31
Denis Malinovsky Avatar answered Oct 26 '22 09:10

Denis Malinovsky