You could do this (but maybe ask yourself why):
user.attributes.key(user.first_name) ...will return "first_name" as a String.
(Edited to reflect better answer by MrTheWalrus below. Please upvote his comment rather than this post).
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about CollectivesStack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
Explore Stack InternalYou could do this (but maybe ask yourself why):
user.attributes.key(user.first_name) ...will return "first_name" as a String.
(Edited to reflect better answer by MrTheWalrus below. Please upvote his comment rather than this post).
You could do this (but maybe ask yourself why):
user.attributes.key(user.first_name) ...will return "first_name" as a String.
(Edited to reflect better answer by MrTheWalrus below. Please upvote his comment rather than this post).
You could do this (but maybe ask yourself why):
user.attributes.key(user.first_name) ...will return "first_name" as a String.
You could do this (but maybe ask yourself why):
user.attributes.select {|_,v| v == key(user.first_name }.keys.first) ... which willwill return "first_name" as a String.
Are you looking for some way of determining which attribute matches a given input?
class User < ActiveRecord::Base #... def attribute_name_by_value(value) attributes.each {|k,v| return k if v == value } nil end end Then user.attribute_name_by_value("[email protected]") would return "email"(Edited to reflect better answer by MrTheWalrus below. Please upvote his comment rather than this post).
You could do this (but maybe ask yourself why):
user.attributes.select {|_,v| v == user.first_name }.keys.first ... which will return "first_name" as a String.
Are you looking for some way of determining which attribute matches a given input?
class User < ActiveRecord::Base #... def attribute_name_by_value(value) attributes.each {|k,v| return k if v == value } nil end end Then user.attribute_name_by_value("[email protected]") would return "email".
You could do this (but maybe ask yourself why):
user.attributes.key(user.first_name) ...will return "first_name" as a String.
(Edited to reflect better answer by MrTheWalrus below. Please upvote his comment rather than this post).
You could do this (but maybe ask yourself why):
user.attributes.select {|_,v| v == user.first_name }.keys.first ... which will return "first_name" as a String.
Are you looking for some way of determining which attribute matches a given input?
class User < ActiveRecord::Base #... def attribute_name_by_value(value) attributes.each {|k,v| return k if v == value } nil end end Then user.attribute_name_by_value("[email protected]") would return "email".