ruby on rails - Define which attribute to work on by passing it as an argument? -
i have class has 2 attributes of same type.
the class have 2 methods doing same thing, 1 each attribute.
is possible cut down these 2 methods 1 passing attribute name argument?
this have tried (but gives me "nomethoderror: undefined method `attribute_name' ..."):
def my_method(attribute_name) self.attribute_name = 2 self.save end
to save without validation:
class="lang-ruby prettyprint-override"> update_attribute(attribute_name.to_sym, 2) to save validation:
class="lang-ruby prettyprint-override"> update_attributes({ attribute_name.to_sym => 2 }) to assign without saving:
class="lang-ruby prettyprint-override"> assign_attributes({ attribute_name.to_sym => 2 }) i think lastly method looking for
class="lang-ruby prettyprint-override">def my_method(attribute_name) self.assign_attributes({ attribute_name.to_sym => 2 }) self.save end ruby-on-rails ruby
No comments:
Post a Comment