ndemoreau
ndemoreau

Reputation: 3869

ruby activerecord easy one: call attribute by variable

I want to update an attribute defined by a variable in an activerecord model.

This is my method

def increment_attribute(attr_name)
  self.attribute(attr_name).increment
end

This doesn't work because attribute is a private activerecord method

How should I do this? Thanks!

Upvotes: 1

Views: 246

Answers (1)

cantlin
cantlin

Reputation: 3226

def increment_attribute(name)
  self.send(name).increment
end

Upvotes: 3

Related Questions