Set Attribute Dynamically of Ruby Object
Use send:
def set_property(obj, prop_name, prop_value)
obj.send("#{prop_name}=",prop_value)
end
Adding new attribute to Object after it's created using method (Ruby)
Ruby is very lax in this regard. Simply access a variable with @
and if it doesn't exist it will be created.
def set_sell_by
@sell_by = value
self
end
Ruby - How to set value for dynamic attribute in class
You should call
a.send "#{attr}=", 1
a.save!
Ruby - dynamically add property to class (at runtime)
The method_missing
approach would work, but if you're going to use the accessors a lot after adding them, you might as well add them as real methods, like this:
class Client
def add_attrs(attrs)
attrs.each do |var, value|
class_eval { attr_accessor var }
instance_variable_set "@#{var}", value
end
end
end
This will make them work like normal instance variables, but restricted to just one client
.
Is it possible to dynamically call an attribute?
If you're using ActiveRecord, you can use []
to get an attribute.
person = Person.last
attr = "height"
puts person[attr]
But this will only work for attributes in Person.columns_hash
and won't work for things like associations.
For that, you'll want to use public_send
, like,
person = Person.last
attr = 'height'
puts person.public_send(attr.to_sym)
If you're going to allow a user to select what attribute this is, you'll want to whitelist so someone doesn't attr = params[:attribute]
and call ?attribute=destroy
and destroy a record. Unless you want them to... :)
Adding dynamic attributes to a Rails model
The write_attribute method updates the attribute in the underlying table. Since your new attributes are dynamic and do not match fields from your model table, no wonder it does not work.
To add attribute dynamically you need to declare it first, and then to call setter as for regular attribute. For example:
attr_name = 'value1'
# Declaring attr_accessor: attr_name
drow.instance_eval { class << self; self end }.send(:attr_accessor, attr_name)
drow.send(attr_name + '=', 'xxx') # setter
drow.send(attr_name) # getter
The property will be saved to the instance variable @value1
.
The other way to save dynamic property is to alter that variable directly, without declaring attribute accessor:
drow.instance_variable_set("@#{attr_name}".to_sym, 'xxx') # setter
drow.instance_variable_get("@#{attr_name}".to_sym) # getter
Related Topics
Combinatory Method Like Tap, But Able to Return a Different Value
Difference Between Map and Each
Rails: Modifying a Model Generated by Scaffolding
Is 'Yield Self' the Same as Instance_Eval
Rubygems + Cygwin: Posix Path Not Found by Ruby.Exe
How to Take Screenshots of Web Pages Using Ruby and a Unix Server
Rails 4 Before_Action, Pass Parameters to Invoked Method
Concatenating String with Number in Ruby
Calling Method in Parent Class from Subclass Methods in Ruby
Ruby Check If Nil Before Calling Method
Implementation of "Remember Me" in a Rails Application
Adding Attributes to a Ruby Object Dynamically
Error Loading Rubygems Plugin ,Openssl.Bundle (Loaderror)
Guidelines for Where to Put Classes in Rails Apps That Don't Fit Anywhere
How to Call a Super Class Method
Why 6.84 - 3.6 == 3.2399999999999998
How to Globally Configure Rspec to Keep the '--Color' and '--Format Specdoc' Options Turned On