Thursday, 3 December 2020

Thursday, 27 August 2020

Generate Dynamic File with Content in rails

 EmailTemplate.where("locale = 'de'").each do |et|

 body = et.body

 File.open("templates/#{et.slug}.html", 'w+') {|f| f.write(body) }

end

Tuesday, 18 August 2020

set root password in mysql for root user

 ~$ sudo mysql

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';

mysql> exit

$ mysql -u root -p

$ Enter Password: root

Tuesday, 4 August 2020

Avoid N+1 Queries

1 . Location.includes(:users).where(users: {username: "ashish"}) 
Lead.includes(:contacts).where("contacts.primary" =>true)

2. Person.includes(notes: [:grades]).where(notes: {important: true, grades: {important: true})

3. 
  class Person < ActiveRecord::Base
has_many :notes
has_many :important_notes, -> { where(important: true) }, class_name: "Note"
end

Person.preload(:important_notes)
4. Person.eager_load(:notes)

Wednesday, 11 December 2019

Generate PDF with rake task



add these gems  in your gem file

gem 'wicked_pdf'
gem 'wkhtmltopdf-binary-edge', '~> 0.12.5.1'
gem "combine_pdf"

and run bundle

In views create directory pdf

create a partial file  _index.pdf.erb
<h1>Hello <%=  name %></h1>

in rake task write below-given code

    ac = ActionController::Base.new()
    header_html = ac.render_to_string(partial: 'pdf/_index.pdf.erb', locals: { name: 'Emissions Export'} )
    pdf = WickedPdf.new.pdf_from_string(header_html, orientation: 'Landscape', margin: { bottom: 20, top: 30 })

  save_path = Rails.root.join('public','mytest.pdf')
    File.open(save_path, 'wb') do |file|
      file << pdf
    end

------------------------------------------------------------------------
if you want to combine two pdf file in one pdf using below given code

pdf = WickedPdf.new.pdf_from_string(header_html, orientation: 'Landscape', margin: { bottom: 20, top: 30 })
    pdf1 = WickedPdf.new.pdf_from_string(header_html, orientation: 'Landscape', margin: { bottom: 20, top: 30 })
    #
    final_pdf = CombinePDF.new
    final_pdf << CombinePDF.parse(pdf)
    final_pdf << CombinePDF.parse(pdf1)

    save_path = Rails.root.join('public','mytest.pdf')
    File.open(save_path, 'wb') do |file|
      file << final_pdf.to_pdf
    end




Tuesday, 12 November 2019

Add Swap memory to ec2 micro instance.


Remember one thing If ec2 instance memory is 1 GB then add 512 MB swap. It should be half of the instance memory.
To add this extra space to your instance you type:
sudo /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
sudo /sbin/mkswap /var/swap.1
sudo chmod 600 /var/swap.1
sudo /sbin/swapon /var/swap.1
If you need more than 1024 then change that to something higher.
To enable it by default after reboot, add this line to /etc/fstab:
/var/swap.1   swap    swap    defaults        0   0

Revert last commit or second last and more....

 Git revert commit_id -m 1 this command willl revert last commit  Git revert commit_id -m 2 this command will revert second commit with same...