Interview Questions Ruby on Rails Developer – Part 5

Tram Ho

About callbacks

Name the callbacks

Callback is an Active Record method that allows you to execute previously declared methods automatically before (or after, or both before and after) another piece of code in the program is run.
The running order of callbacks for each action is shown in the below.

CREATEUPDATE
before_validationbefore_validation
after_validationbefore_validation
before_savebefore_save
around_savearound_save
before_createbefore_create
around_createaround_create
after_createafter_create
after_saveafter_save
after_commit / after_rollbackafter_commit / after_rollback
DELETEOTHER
before_destroyafter_commit / after_rollback
around_destroybefore_action and after_action
after_destroyafter_initialize and after_find

How to use?

Use callback
before_action: method_callback, only / except: action_must_callback
Or callback: method_callback

Skipping Callbacks
Using the following methods will ignore callbacks:

  • decrement
  • decrement_counter
  • delete
  • delete_all
  • increment
  • increment_counter
  • toggle
  • update_column
  • update_columns
  • update_all
  • update_counters

However, we should be cautious about using these methods because important business rules and application logic can be kept in callbacks. By transferring them without understanding the potential implications may result in invalid data.

What are Strong params?

Strong Parameters is a method to deal with security errors when using the Mass Assignment feature. What is the strong params for? Strong parameters prevent us from controlling the values ​​in the params.

  • require (: user): required in params must be user key
  • permit (: name,: email) to specify the attributes that will pass in the params. If passing the attribute is not in the permit then it will not be saved when create / update and this property will not be available after running through strong params.

What are nested attributes?

Nested attributes are a technique used to create / remove these records when creating / updating other records; usually applies to objects that have has_one or has_many relationships.

Mechanism of nested attribute

To use it, activate it using the accepts_nested_attributes_for method in the corresponding model. Some customization when using accepts_nested_attributes_for :

: allow_destroy

true or false, if true, the record may be deleted

: reject_if

Lets you specify a Proc or a Symbol to point to a method to check whether a record is allowed to be created

: limit

Allows you to specify the maximum number of related records that can be processed with nested attributes: the limit option only applies to one-to-many relationships.

: update_only

Allows you to specify a record to be updated only, if that record already exists; when it doesn’t exist, you can create. Only works for one-to-one relationships.

Mechanism of creating a record

For example: User has one Avatar

When the new user gets the params, it looks like this:

Some basic gem types

To use gem, we need to declare that gem in Gemfile file with the following syntax

Then run bundle install again to install the gem and use it
Some gems need to create files for installation. For example, gem config below

Gem Pry-Rails

Do you want to debug ??? https://github.com/pry/pry binding.pry

Gem Config

Gem config helps manage and easily use the settings in the project. Put simply, all variables and values ​​needed to configure the system are centrally declared in one or several files.
https://github.com/railsconfig/config

Gem figaro, dotenv

Manage the entire environment variable of the project
http://railsapps.github.io/rails-environment-variables.html

Gem kaminari

Use for pagination
https://github.com/amatsuda/kaminari Used with gem “bootstrap4-kaminari-views”

Pagy (replace kaminari)

Use for pagination
https://github.com/ddnexus/pagy

Gem Devise

Devise is a very flexible gem used in user authentication
https://github.com/plataformatec/devise

Gem CanCanCan

Authorizing permissions for ruby ​​on rails, it restricts the allowed resources
https://github.com/CanCanCommunity/cancancan

PAPERCLIP or CarrierWave

2 Gem are used to upload photos or files for your project, the use of both are not too complicated, but I recommend carrierwave.

Gem Ransack

Searching is simple and incorporates many search terms
https://github.com/activerecord-hackery/ransack

Background job: sidekiq, rescue, delayed job

https://github.com/mperham/sidekiq
https://github.com/resque/resque
https://github.com/codez/delayed_cron_job

Share the news now

Source : Viblo