Undo Local changes in Git

git reset —hard

git reset --hard allows you to remove all changes to local files. This is final.

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   vue-cart/index.html

no changes added to commit (use "git add" and/or "git commit -a")
$ git reset --hard
HEAD is now at b25f597 Creating a simple Cart
$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

git reset HEAD

git reset HEAD <file> allows you reset changes to the last commit. This is final.

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   vue-cart/main.js

no changes added to commit (use "git add" and/or "git commit -a")
$ git reset HEAD vue-cart/main.js 
Unstaged changes after reset:
M	vue-cart/main.js

 PG::ConnectionBad (could not connect to server)

PG::ConnectionBad (could not connect to server: Connection refused
	Is the server running on host "localhost" (::1) and accepting
	TCP/IP connections on port 5432?
could not connect to server: Connection refused
	Is the server running on host "localhost" (127.0.0.1) and accepting
	TCP/IP connections on port 5432?
)

This is something that most likely resulted when a PostgreSQL server was updated but the databases weren’t updated to the current version.

Here is a simple fix that could help you.

brew postgresql-upgrade-database

 brew postgresql-upgrade-database
==> brew install postgresql@10
==> Downloading https://homebrew.bintray.com/bottles/postgresql@10-10.10_1.mojave.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/0b/0bfd1011bd9500221b4213c86699a13c692acdf41984e88a1b86f2397ea11f15?__gda__=exp=1568557647~hmac=02a02995ee6bde2ee4d6871f6974b36d
######################################################################## 100.0%
[......]
==> Upgraded postgresql data from 10 to 11!
==> Your postgresql 10 data remains at /usr/local/var/postgres.old
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)

Everything would hopefully be working for you too!

 Capybara Cheatsheet

Navigation

visit('/posts')
visit(post_path(post))

Clicking Links and Buttons

click_link('id-of-link')
click_link('Link Text')
click_button('Save')
click('Link Text')
click('Button Value')

Interacting with Forms

fill_in('First Name', :with => 'Mark')
fill_in('Password', :with => 'password')
fill_in('Description', :with => 'Description')
choose('radio button')
check('checkbox')
uncheck('checkbox')
attach_file('image field', '/path/to/image.png')

 Need Counter Cache from Bullet Gem

Counting data is something we normally do. We count cart items in e-commerce websites, answers to questions in question and answer websites. Bullet is a gem that helps you increase your application’s performance by reducing the number of queries it makes.

Here is an example of a request that has a N+1 issue

Question Load (123.0ms)  SELECT "questions".* FROM "questions"
[active_model_serializers]    (0.9ms)  SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1  [["question_id", 9]]
[active_model_serializers]       app/serializers/question_serializer.rb:7
[active_model_serializers]    (3.6ms)  SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1  [["question_id", 2]]
[active_model_serializers]       app/serializers/question_serializer.rb:7

Bullet will show this in the log as:

[active_model_serializers]       app/serializers/question_serializer.rb:7
user: theuser
GET /api/v1/questions
Need Counter Cache
  Question => [:answers]

Need Counter Cache

Here is how to do caching counters with ActiveRecord’s counter caches.

To create a counter cache, we should add a field to the parent table. In this case, it is the question table.

class AddAnswersCountToQuestions < ActiveRecord::Migration[5.2]
  def change
    add_column :questions, :answers_count, :integer
  end
end

When we request from our application, we can see that only one query is applied

Started GET "/api/v1/questions" for ::1 at 2019-08-31 09:13:55 +0800
Processing by Api::V1::QuestionsController#index as JSON
  Question Load (8.5ms)  SELECT "questions".* FROM "questions"
    app/controllers/api/v1/questions_controller.rb:8
[active_model_serializers] Rendered ActiveModel::Serializer::CollectionSerializer with ActiveModelSerializers::Adapter::Attributes (6.34ms)
Completed 200 OK in 84ms (Views: 61.6ms | ActiveRecord: 18.8ms)