First, make sure you've made at least one topic on the site.
Next, open a Rails console in a terminal window:
Expected result:$ rails console
Running via Spring preloader in process 1234
Loading development environment (Rails 5.1.2)
irb(main):001:0>
To get out of the console, type exit
.
At the console, try the following things
See how many topics exist:
Save the first topic into a variable:
IRBmy_topic = Topic.first
my_topic
here could have been any variable name, but we'll stick with my_topic
for consistency.
Change the title of that topic to something else:
IRBmy_topic.update(title: 'Edited in the console')
Add a vote to that topic:
See how many votes that topic has:
Remove a vote from that topic:
IRBmy_topic.votes.first.destroy
Note that the things you can do to Model classes (like Topic and Vote), differ from the things you can do to Model instances (like my_topic
, here). my_topic.votes
is an association, and here behaves mostly like a model class.
Model class / association methods
- Topic.first
- Topic.last
- Topic.all
- Topic.count
- Topic.find_by_id(5)
- Topic.destroy_all
- my_topic.votes.count
- my_topic.votes.create
- my_topic.votes.destroy_all
Model instance methods
- my_topic.title
- my_topic.title = 'New title'
- my_topic.update(title: 'New title')
- my_topic.save
- my_topic.save!
- my_topic.destroy
- my_topic.votes.first.destroy