How to create a new object, pass it some parameters, and then save it

I want to create a new object, pass some initial values ​​to it, and then save in db and return the saved object.

How can i do this?

Example:

  • create a new custom object
  • initialize user.user_age property to 35
  • save and return the saved b / c object I need access to the value of user.user_id.

What is the ruby ​​way to do this?

+4
source share
1 answer
user = User.create(:user_age => 35) 

This will initialize the new object, set the user_age attribute to 35, save it in the database, and return the User object that represents this entry in the database.

+5
source

All Articles