Custom Field not working

I am going to answer my own question here, since I kind of understand where I was making mistake in terms of my thinking process, and also a benefit of getting feedback from the rest of the team here, as well as for the benefit of new comers to custom_fields functionality in Discourse.

Basically I was making a mistake in thinking without investigation that after I create a custom field on User object by doing

user = User.find(1)
user.custom_fields[“test_field”] = " X X X X"
user.save

I was expecting the custom field to be created on the user object i.e. to become

custome_field_test_field

and I was doing:

user.custom_fields_test_field

But after investigating deeply, I found out that, custom_field is actually an object

user.custom_fields[“test_field”] = { }

and when I do

user.custom_fields[“test_field”] = " X X X X"

it appends it to the hash/object

Therefore it becomes

user.custom_fields = {
"test_field"] = " X X X X"
}

3 Likes