TIL about Streaming and Server Sent Event
Streaming
![](https://til.dev/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBRdz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--ca69fb8750b9a7fa3d0d32e17f276bd78634834a/Screenshot%202024-03-01%20at%2011.32.52%E2%80%AFPM.png)
Server Sent Events
![](https://til.dev/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBSQT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--4d66cccc20a0fbf6b10a089214443dc8ad55c98f/Screenshot%202024-03-01%20at%2011.33.41%E2%80%AFPM.png)
![](https://til.dev/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBSUT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--19abbdd6d91af14ddd68e4cb549b071f9297e7ba/Screenshot%202024-03-01%20at%2011.34.19%E2%80%AFPM.png)
TIL in rails: `Time.current.all_month` to get the whole range
![](https://til.dev/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBRUT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--47e635a48ff1093354a0597e661c18c235aa7c9b/Screenshot%202024-02-16%20at%206.29.34%E2%80%AFPM.png)
https://api.rubyonrails.org/classes/DateAndTime/Calculations.html#method-i-all_month
Ruby double equal in views <%== pagy_info(@pagy) %>
Here is what I found:
- <% %>: Executes the Ruby code within the brackets but does not print the result to the template.
- <%= %>: Executes the Ruby code and prints the result to the template, with HTML escaping.
- <%== %>: Executes the Ruby code and prints the result to the template without HTML escaping.
So, in an example that prompted me to search, pagy_info(@pagy) is being called to generate pagination information, and the == ensures that the HTML generated by pagy_info is rendered as-is in the view, allowing for proper display of pagination links or information without escaping the HTML tags.
"be", "eq" and "eql", "equal" are different in rspec
TIL we can do `array1 | array 2` to merge them and get unique ones
irb(main):001> ["a", "b"] | ["b", "c"] => ["a", "b", "c"]
TIL in Rails: How to change column defaults with :from and :to option for making it a reversible migration
def change change_column_default :blog, :language, "en_US" end
and it gave this error when I try to rollback the migration
Caused by: ActiveRecord::IrreversibleMigration: change_column_default is only reversible if given a :from and :to optio
SOLUTION:
def change change_column_default :blog, :language, from: "en", to: "en_US" end
TIL in rails: `Model.none` returns a chainable relation with zero records
TIL about unaccent (PostgreSQL extension)
SQL unaccent is a PostgreSQL extension that removes accents (diacritic signs) from lexemes. It is a filtering dictionary that can be used to improve user experience and implement search in a user-friendly way. The unaccent() function removes accents from a given string and can be used outside normal text search contexts. For example, SELECT unaccent('unaccent', 'Hôtel') returns 'hotel'. The unaccent extension provides a single function, unaccent(), which can be used to remove accents from words. It can even be indexed using an unaccent-based index to speed up the process.
TIL Variable hoisting in ruby
I might be still continue declaring explicity as it gives more clarity to me but still good to know about hoisting in ruby :)
You can read more here: https://ieftimov.com/posts/variable-hoisting-ruby/
TIL about 'bin/rails runner' in Rails
TIL in Ruby: `arity`
Returns the number of mandatory arguments.
If the block is declared to take no arguments, returns 0.
If the block is known to take exactly n arguments, returns n.
If the block has optional arguments, returns -n-1
[Ruby][Rails] Learned about a new gem decent_exposure
TIL about PostgreSQL follower database in Heroku
TIL in Flutter, we can specify the app version and build number in pubspec.yaml
![](https://til.dev/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBOUT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--cd4f3da57ed53075bf39153a8f7a4ce6ee6d6bbe/Screenshot%202022-05-07%20at%207.58.47%20AM.png)
TIL in Javascript, Date `getMonth()` does not give the correct month as we expect 🤯
today = new Date() Thu May 05 2022 19:28:34 GMT+0530 (India Standard Time) today.getMonth() 4
what! it should be 5 (May!)
looks like the return values are from 0 to 11
so for now we'll have to do today.getMonth() + 1 to get correct month value
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getMonth#return_value
TIL we can compare versions in Ruby using `Gem::Version` without any gems
Gem::Version.new('1.0.1') > Gem::Version.new('1.0.0')
Here is the link to the documentation: https://ruby-doc.org/stdlib-3.1.0/libdoc/rubygems/rdoc/Gem/Version.html
TIL How to change screenshot directory in macOS
defaults write com.apple.screencapture location {location here}
e.g,
defaults write com.apple.screencapture location /Users/rinas/Desktop/Screenshots
TIL in Rails: Code after redirect_to gets executed
redirect_to url and return
Here is the issue which talks about this: https://github.com/rails/rails/issues/26363
TIL about using counter_cache in rails to save association's count in parent model
Here is the SO answer that helped: https://stackoverflow.com/a/16996960/3098242
![](https://til.dev/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBKdz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--b85aeead479395e3efd130cdb5394ee7ae2b1ad1/zipmcwlqux2shw7lu36wojl8ldxf.png)
TIL how to select values from one column without `nil` in Rails
Page.where.not(custom_domain: nil).pluck(:custom_domain)
---------------------------------------
Page.pluck(:custom_domain)
![](https://til.dev/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBKQT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--eed5720b39011a9201f758bcce552f0311625d07/urlwbac03c7cjseb7xxb5gnj0jn2.png)
`.pluck` picks values but includes `nil` as well. So we can use `.compact`
Page.pluck(:custom_domain).compact
![](https://til.dev/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBJdz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--4220ce6cf69a96bc96497398ae61e5bb7f1c2ed0/lx2mh693o1q2bdf3kcmgmigbwxd4.png)
TIL How to change progress bar colour in Rails
TIL what policy to use for AWS S3 to work with Rails Active Storage
![](https://til.dev/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBJUT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--952f03aa3244aef1ffc73e55f1292672bdbbd298/oz0tj0ornwpg3ohm5tn2giwhdxst.png)
TIL how to colour console logs
console.log('%c hey red text', 'color: red') console.log('%c hey green text', 'color: green')
![](https://til.dev/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBIdz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--188636d96394f91eae914be76d5956887a9ac817/m8itfgw7m84mjran4kdvyyjko9bo.png)