How can I get the attribute from the model object dynamically? I have the attribute of the User object as string:
u = User.find(1)
Can I do something like u.get("user_id")
You could try using the ActiveRecord model instance like a hash.
u = User.find(1)
name = u[:name]
field = "first_name"
first_name = u[field]
<active_record_class>.find(id) and then use the attribute value from the fresh copy as described here by @aditya-sanghi. The read_attribute method does not hit the database.Try this
user = User.find(1)
then something like this should do what you require
user.send('field_name')
u.user_idor one of several other simpler forms.