-1

My user has the attribute:

:step1_local
:step2_local
:step3_local
...
...
:local1
:local2
:local3

I would like to change an attribute value based on another set of attributes on the same model. I would like to do some processing mapping on user, say:

def magic (user)
   user.local(1..3) = process(user.step(1..3)_local)
end

The code above of course does not work (example). I am not sure how to do it dynamically without going through each attributes individually. I want to map processing one to another. Any ideas?

4

1 に答える 1

2

Object#public_send次のように and メソッドを使用できます。

def magic(user)
  (1..3).each do |n|
    user.public_send("local#{n}=", process(user.read_attribute("step#{n}_local")))
  end
end
于 2013-09-30T07:31:44.153 に答える