-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path32_Models_and_rails_console.txt
More file actions
35 lines (22 loc) · 1.37 KB
/
32_Models_and_rails_console.txt
File metadata and controls
35 lines (22 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Models and rails console - text references
----------------------------------------------------------------------------------
To create an article model, create an article.rb model file under app/models folder and fill it in:
class Article < ApplicationRecord
end
Note: Make sure ApplicationRecord is CamelCase.
Now, provided you have the articles table already, you can use the Rails console and work with the articles table using this article.rb model file.
To start the rails console, type in rails console or rails c from the terminal.
Once in the console, you can exit it at any time by typing in exit followed by enter/return.
You can test out your connection to the articles table by typing the following command from within your rails console:
Article.all
If you get an empty collection/array-like structure as a response, you're good to go.
To create a new article, you can use any of the following methods:
Article.create(title: "first article", description: "Description of first article") # make sure Article is capitalized if using this method
article = Article.new
article.title = "second article"
article.description = "description of second article"
article.save
article = Article.new(title: "third article", description: "description of third article")
article.save
To check all the articles that exist in your articles table, you can use the following command:
Article.all