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.
CREATE | UPDATE |
---|---|
before_validation | before_validation |
after_validation | before_validation |
before_save | before_save |
around_save | around_save |
before_create | before_create |
around_create | around_create |
after_create | after_create |
after_save | after_save |
after_commit / after_rollback | after_commit / after_rollback |
DELETE | OTHER |
---|---|
before_destroy | after_commit / after_rollback |
around_destroy | before_action and after_action |
after_destroy | after_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
1 2 | user_params <span class="token operator">=</span> <span class="token keyword">require</span> <span class="token punctuation">(</span> <span class="token symbol">:user</span> <span class="token punctuation">)</span> <span class="token punctuation">.</span> permit <span class="token symbol">:username</span> <span class="token punctuation">,</span> avatar_attributes <span class="token punctuation">[</span> <span class="token symbol">:id</span> <span class="token punctuation">,</span> <span class="token symbol">:url</span> <span class="token punctuation">]</span> |
When the new user gets the params, it looks like this:
1 2 3 4 5 6 7 8 9 10 | <span class="token punctuation">{</span> user <span class="token punctuation">:</span> <span class="token punctuation">{</span> username <span class="token punctuation">:</span> <span class="token string">'A'</span> <span class="token punctuation">,</span> avatar_attributes <span class="token punctuation">:</span> <span class="token punctuation">{</span> url <span class="token punctuation">:</span> <span class="token string">'abc.jpg'</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> user <span class="token operator">=</span> <span class="token constant">User</span> <span class="token punctuation">.</span> <span class="token function">create</span> <span class="token punctuation">(</span> params <span class="token punctuation">[</span> <span class="token symbol">:user</span> <span class="token punctuation">]</span> <span class="token punctuation">)</span> |
Some basic gem types
To use gem, we need to declare that gem in Gemfile file with the following syntax
1 2 | gem <span class="token string">"name of gem"</span> <span class="token punctuation">,</span> <span class="token operator">~</span> version |
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
1 2 | rails g config <span class="token symbol">:install</span> |
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