Since I started learning rails and used the rails console more often, I will summarize it here.
rails c
quit
As an example, the model name is ʻUser`.
User.all
users = User.all
users.find(1)
users = User.all
users.find_by(id:1) # users.find(1)Same meaning as
When searching by user name As an example, the column name is ʻusername`.
users.find_by(username:'Naoki')
user.save
Specific example: Rewriting the name
user = User.find(1)
user.username = 'Baikinman'
user.save
user.destroy
Specific example: Delete record
user = User.find(1)
user.destroy
[Introduction to Rails] Summary of how to use console Manipulate Model in Rails Console
After adding data to the database with the app, basically it seems that there is no problem if you remember this operation method. We look forward to helping you.
Recommended Posts