So if I understand you right then you are probably trying to map the
of the column back to the column name.
eg.If you have a table like
id, name, age
1, “Alice”, 25
2, “Bob” , 35
Then given row for id 2, if you have a Fixnum 35 you want to be able to
string “age”. Is that right?
I am not a Rails expert but you could perhaps use column_names method on
your bar could look like
def bar(active_record, value)
active_record.attribute_names.each do |x|
return active_record.column_for_attribute(x).name if
and call it with bar(@user, @user.age) as in your example.
There is a big caveat though and that is since we are comparing values
could be more than one column of the same value and this snippet matches
Without going into specifics of what you need you should perhaps look
some alternate approach which could come from your domain use case.