Posts

Showing posts from October, 2020

The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received an instance of Array

 res.end() to res.send()

Some Ruby on Rails interview questions

 1) How flash message work in ruby on rails? Ans. They are stored in your session store. The default since rails 2.0 is the cookie store, but check in config/initializers/session_store.rb to check if you're using something other than the default. According to APIdock : ActionController/Flash, it is stored in a session. Note that if sessions are disabled only flash.now will work. When using flash.now, your values are not available in the next request. 2) What is the use of Asset-PipeLine in Rails? Ans. Assets(css, scss, js, coffeescript, images etc), its fast executed in rails application because of asset pipeline when we compile it or when its compiled, it will generate a single source code file or a manifest file, so that's why assets are interpret fastly. 3)   Difference between find and find_by? Ans . The find method is usually used to retrieve a row by ID:            Model.find(1)           And it will throw ActiveRecord::RecordNotFound error if record not found. find_by is

Some important and unknown(least used) ruby methods.

 1) inject or reduce      When called, the inject method will pass each element and accumulate each sequentially.     ex:         [1,2,3].inject(:+)    => ((1+2)+3))   //we have can any operator here i.e +,-,*,/,%         OR         [1,2,3].inject{|sum, value| sum + value } => 1 iteration:  (1 + 2)                                                                                             =>  2 iteration: (3(1st iteration sum)+3)            we  can also pass it a default value or base value for the accumulator.     ex:          [1,2,3].inject(0, :+)    => (0+(1+2)+3)) //so here initial value for sum is 0.          We can also use inject menthods for building hash's from the array's     ex:           [[:book, "data structure"], [:price, "400rs"]].inject({}) do |result, element|               result[element.first] = element.last               result           end            #=> {:book=>"data structure", :price=>"400rs&qu

Setup a cron job or scheduler for your rails application using whenever gem.

cd path to your project. gem 'whenever', require: false   add it to your gemfile run bundle install bundle exec wheneverize .   //This will create an initial config/schedule.rb Now create your job call it  from config/schedule.rb whenever --update-crontab   //update crontab crontab -l   //list all cron job set :output, "log/cron.log"    //if you want to set log file for cron job then that this line at the top of config/schedule.rb         in production server:                   after deployment :  bundle exec whenever whenever --update-crontab crontab -l source: https://github.com/javan/whenever https://www.rubyguides.com/2019/04/ruby-whenever-gem/