# How to write a ruby code that add a new user's data to a user\_custom\_fields

**URL:** <https://meta.discourse.org/t/how-to-write-a-ruby-code-that-add-a-new-users-data-to-a-user-custom-fields/66662>\
**Category:** Product Support\
**Created:** [July 23, 2017, 9:04am UTC](https://meta.discourse.org/t/how-to-write-a-ruby-code-that-add-a-new-users-data-to-a-user-custom-fields/66662 "2017-07-23T09:04:15Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![Alavi1412](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alavi1412/32/67721_2.png) [@Alavi1412](https://meta.discourse.org/u/Alavi1412)\
**Post date:** [July 23, 2017, 2:10pm UTC](https://meta.discourse.org/t/how-to-write-a-ruby-code-that-add-a-new-users-data-to-a-user-custom-fields/66662/4 "2017-07-23T14:10:33Z")

</div>

I find the answer.

Adding a new row or editing older rows in Ruby On Rails isn’t like PHP because Rails is so much objective even when you want to work with database.

I will explain my answer for other who have my problem:

FORGET about sql query, in Here everything are objects, every row in database is an object. for adding a new row, you should create a new object and if you want to edit a row, you should find that row as an object.

So if everything is object here, What is the class name for creating new one?  
If you want to edit the `user_custom_fields` table, the class name is `UserCustomField`.  
to edit an exiting data, you should find it first like this:

```plaintext
MyObject = UserCustomField.where(name: "user_field_5", user_id: 364).first
MyObject.value = "new Value"
MyObject.save

```

this code will change the custom\_user\_field with name `user_field_5` and user\_id `364` .  
now for adding new one you should do this:

`NewObject = UserCustomField.create(user_id: 634, name: 'user_field_6', value: 'my value')`  
this code will create a new row in database and you shouldn’t define `id`, `created_at` , `updated_at` because rails will define them automatically and also increase the sequence. 😉

---

_[View the full topic](https://meta.discourse.org/t/how-to-write-a-ruby-code-that-add-a-new-users-data-to-a-user-custom-fields/66662)._
