Rinas 🧑‍💻

@rinas

Website GitHub X

Check cache exists and value in rails console

# Check if cache exists
Rails.cache.exist?('latest_posts')

# Read the current cached value
Rails.cache.read('latest_posts')

# Delete the cache if needed
Rails.cache.delete('latest_posts')

TIL in rails how to check which database a model is connected to from rails console

ActiveRecord::Base.connection_db_config.name

Car.connection_db_config.name
Vehicle.connection_db_config.name

This is useful in scenarios where we have models connecting to different database and want to check them via rails console

TIL in Flutter we can do "flutter pub deps" to list all dependencies used in a nice format.

Doing "flutter pub deps" shows all dependencies like this:

GZNhzSuW4AA125z.jpeg 125 KB

TIL how to check index creation progress in postgres

SELECT 
   (blocks_done::numeric / blocks_total::numeric) * 100 AS percentage_done
FROM     
   pg_stat_progress_create_index;




Got this from Stackoverflow: https://stackoverflow.com/a/78640729

Yes, it's from the good old stackoverflow :)

TIL about Streaming and Server Sent Event

Streaming


class MyController < ActionController::Base
  include ActionController::Live

  def stream
    response.headers['Content-Type'] = 'text/event-stream'
    100.times {
      response.stream.write "hello world\n"
      sleep 1
    }
  ensure
    response.stream.close
  end
end


Server Sent Events



class MyController < ActionController::Base
  include ActionController::Live

  def index
    response.headers['Content-Type'] = 'text/event-stream'
    sse = SSE.new(response.stream, retry: 300, event: "event-name")
    sse.write({ name: 'John'})
    sse.write({ name: 'John'}, id: 10)
    sse.write({ name: 'John'}, id: 10, event: "other-event")
    sse.write({ name: 'John'}, id: 10, event: "other-event", retry: 500)
  ensure
    sse.close
  end
end



Example Usage of EventSource in JavaScript:
https://developer.mozilla.org/en-US/docs/Web/API/EventSource

TIL in rails: `Time.current.all_month` to get the whole range

Ruby double equal in views <%== pagy_info(@pagy) %>

Encountered <%== pagy_info(@pagy) %> today and was wondering that the double equal (==) mean

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

"be", "eq" are same, and checks for the value of the object

"eql", "equal" are same, and checks for object_id of the object


Here is where I learend this from:

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

I was trying to change a column default 
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
Learned that we can pass :from and :to option there. 


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

Although I knew about hoisting in javascript, I always explicitly declare nil while writing ruby and was under the impression it will give undefined otherwise.

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

Here is the link to the tweet by @nateberkopec: https://twitter.com/nateberkopec/status/1642965409165877268

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

I was changing the marketing version and build number manually in Xcode but looks like it can be controlled from pubspec.yaml file which is very handy

TIL we can use `xed` to open Xcode with given folder or project

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

We can just do 

Gem::Version.new('1.0.1') > Gem::Version.new('1.0.0')

TIL in Flutter (iOS): No transparent images allowed in assets

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

Heroku cli for rails console

heroku run rails c -a APP_NAME

TIL in Rails: Code after redirect_to gets executed

Since it gets executed, we need to add `return` 

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

I had used counter_cache in the past but still had to look it up :D

Here is the SO answer that helped: https://stackoverflow.com/a/16996960/3098242

-

TIL how to select values from one column without `nil` in Rails

Update:

There is a better way to do this as discussed here in Twitter

Page.where.not(custom_domain: nil).pluck(:custom_domain)


---------------------------------------

Page.pluck(:custom_domain)
Screenshot of rails console

`.pluck` picks values but includes `nil` as well. So we can use `.compact`


Page.pluck(:custom_domain).compact
Screenshot of rails console

TIL How to change progress bar colour in Rails

.turbo-progress-bar {
  background-color: black;
}

You can see it in action here: https://whatsnew.co/

TIL how to set default ruby version using RVM

rvm --default use 3.1.0

TIL what policy to use for AWS S3 to work with Rails Active Storage

TIL how to colour console logs

We can change the colour of log in console like this:

console.log('%c hey red text', 'color: red')
console.log('%c hey green text', 'color: green')

Screenshot of developer console



Regarding privacy concerns, it's simple - we don't sell your data. In fact, we try to use privacy-focused software/services like Fathom Analytics whenever we use any third-party services.