开发者

How to create a new object, passing it some parameters, and then saving it

开发者 https://www.devze.com 2023-01-22 11:08 出处:网络
开发者_如何学PythonI want to create a new object, passing it some initial values, and then saving to the db and return the saved object.

开发者_如何学PythonI want to create a new object, passing it some initial values, and then saving to the db and return the saved object.

how can I do this?

example:

  1. create a new user object
  2. initiaze the user.user_age property to 35
  3. save and return the saved object b/c I need access to the user.user_id value.

What's the ruby way of doing this?


user = User.create(:user_age => 35)

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

0

精彩评论

暂无评论...
验证码 换一张
取 消