From the course: Hands-On Introduction: Ruby on Rails
Unlock the full course today
Join today to access over 24,500 courses taught by industry experts.
Manipulating and querying the model - Ruby on Rails Tutorial
From the course: Hands-On Introduction: Ruby on Rails
Manipulating and querying the model
- [Instructor] Now, you can interact with the database through the model you've created. We'll take the opportunity to do it from the Rails console. Open the terminal and type rails console. Using the Post model, create a new record with the description, "Hello World!" Rails will start a transaction and creates the record with an SQL insert. At the end of the output, you can see how the record looks and all its fields initialized. Now you can execute the statements like Post.all which returns all the posts, note that it's an array, or Post.last which returns only the last one. Let's assume that you want to update the description. It can be done in many ways, but an easy and readable one can be saving the record in a variable. And now in the description, set the new value. Now, save the changes. Notice how Rails has used the SQL update statement. To delete the record, call the destroy method. And now, the SQL Delete statement has been executed. If you list all the posts, there are no…