Posts

#6. Fetch time in rails

 In the Rails do not prefer to use Time.now, Date.today etc We should always prefer to use it with zone like, Time.zone.now, Time.zone.today etc. but why so? Beacause when we fetch Time.now its always return system local time and ignore  config.time_zone  set in the  application.rb. file. Here is the example tested in the rails console, 3.0.3 :001 > Time.now  => 2022-04-19 09:57:58.413034249 +0530  3.0.3 :002 > Time.zone  => #<ActiveSupport::TimeZone:0x000055704feec360 @name="UTC", @tzinfo=#<TZInfo::DataTimezone: Etc/UTC>, @utc_offset=nil>  3.0.3 :003 > Time.zone.now  => Tue, 19 Apr 2022 04:28:07.277000358 UTC +00:00  3.0.3 :004 > Time.zone = 'Sydney'  => "Sydney"  3.0.3 :005 > Time.zone  => #<ActiveSupport::TimeZone:0x000055705192d850 @name="Sydney", @tzinfo=#<TZInfo::DataTimezone: Australia/Sydney>, @utc_offset=nil>  3.0.3 :008 > Time.now  => 2022...

#7. Setting environment variables for rails app in ubuntu

Never keep any secret data in public folder. Always use environment variables to keep secret data. Suppose we want to set postgresql username and password as environment variables  in Ubuntu then do as, # type the following in the terminal to write in the .bashrc file. echo 'export MY_POSTGRESQL_USERNAME="rajkumar"' >> ~/.bashrc echo 'export MY_POSTGRESQL_PASSWORD="HJH34df@dff#@"' >> ~/.bashrc reload .bashrc as, source ~/.bashrc In order to print environment variables prepend it with $ as,  echo $MY_POSTGRESQL_USERNAME echo $MY_POSTGRESQL_PASSWORD test environment variables as, echo $MY_POSTGRESQL_USERNAME rajkumar echo $MY_POSTGRESQL_PASSWORD HJH34df@dff#@ Also use ubuntu command printenv to print all the environment variables. then in the database.yml use these environment variables as, development:   adapter: postgresql   encoding: unicode   host: localhost   username: <%= ENV["MY_POSTGRESQL_USERNAME"] %>   password: ...

#9. Some useful heroku commands

Heroku commands sudo snap install --classic heroku heroku login heroku create git config --list --local | grep heroku git push heroku main heroku run rake db:migrate heroku apps:rename kamb2017 heroku ps:scale web=1 heroku ps heroku open heroku logs heroku logs --tail heroku run rails console heroku config - view  heroku config:set MY_PASS=secret - set heroku config:get MY_PASS - view heroku config:unset MY_PASS - remove heroku config -a rk-rails-app - list all environment variables heroku apps - list all the heroku apps  heroku config -a rk-rails-app --json - list all environment variables in json format heroku run -a rk-rails-app printenv - print environment variables

#5. bundle install error

Image
Today I was getting one error as, Could not load the 'listen' gem. Add `gem 'listen'` to the development group of your Gemfile (LoadError) When I dig it, I found that I run a command previously as,    bundle install --without development test Result of this command, it's saved by default. Refer  here . Now whenever I run  bundle install,  I can see that development and test gems are always ignored like, Here we can see that, development and test environment gems are ignored. In order to solve this error run as, bundle config --delete without After that do bundle install it will install all the gems and that error disappear too.

#4. Sublime settings

Must have these following settings before using sublime text editor for better software development. Many developer use Sublime as text editor for software development. Sublime is easy to use and have lots of features that we need in day to day life of software development. Here are some of the hacks that we can try. Setting #1 Go to, Preferences -> Settings copy and paste the followings code, { "color_scheme": "Packages/Color Scheme - Default/Mariana.sublime-color-scheme", "font_size": 11, "ignored_packages": [ "Vintage" ], "save_on_focus_lost": true, "tab_size": 2, "theme": "Adaptive.sublime-theme", "translate_tabs_to_spaces": true } Setting #2 Go to Preferences -> Settings - Syntax Specific copy and paste the followings code, // These settings override both User and Default settings for the Ruby syntax {   "detect_indentation": false,   "defau...

#2. How to create git alias

git config --global alias.co checkout git config --global alias.br branch git config --global alias.st status File where these settings are stored is,  ~/.gitconfig cat ~/.gitconfig

#3. Git global settings on ubuntu system

git config --global user.name "Raj Kumar" git config --global user.email 2017kamb@gmail.com git config --global core.editor vim git config --global merge.tool meld git config --global  diff.guitool meld git config --list Example, raj@ROR-04:~$ git config --list alias.co=checkout alias.br=branch alias.st=status user.name=Raj Kumar user.email=2017kamb@gmail.com core.editor=gedit merge.tool=meld diff.guitool=meld File where these settings are stored is,  ~/.gitconfig