<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Lazy coder</title>
    <description>The latest articles on DEV Community by Lazy coder (@alazycoder101).</description>
    <link>https://dev.to/alazycoder101</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F639838%2F434ac9be-264f-4c6f-95db-4872e73d209b.jpeg</url>
      <title>DEV Community: Lazy coder</title>
      <link>https://dev.to/alazycoder101</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alazycoder101"/>
    <language>en</language>
    <item>
      <title>Streamlining the Data Symphony: Enhancing Serialization in Ruby on Rails</title>
      <dc:creator>Lazy coder</dc:creator>
      <pubDate>Fri, 02 Aug 2024 10:53:00 +0000</pubDate>
      <link>https://dev.to/alazycoder101/streamlining-the-data-symphony-enhancing-serialization-in-ruby-on-rails-498p</link>
      <guid>https://dev.to/alazycoder101/streamlining-the-data-symphony-enhancing-serialization-in-ruby-on-rails-498p</guid>
      <description>&lt;h1&gt;
  
  
  &lt;strong&gt;Streamlining the Data Symphony: Enhancing Serialization in Ruby on Rails&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;In the intricate ballet of web application development, Ruby on Rails often leads the performance with grace. But when it comes to the grand orchestration of large, nested data structures, even the most agile framework can stumble. Let's embark on a journey to refine the process of serializing complex data, like the multi-layered hierarchy of classrooms, students, lessons, exercises, and scores.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prologue: The Nested Data Conundrum
&lt;/h2&gt;

&lt;p&gt;Picture a vast library where each bookshelf represents a layer of data. The deeper you venture, the more intricate the connections become. Fetching such data in one go can be as daunting as navigating a labyrinth. But fear not, for we have strategies to illuminate the path.&lt;/p&gt;

&lt;h2&gt;
  
  
  Act I: The Cache – A Treasure Trove of Pre-Rendered Delights
&lt;/h2&gt;

&lt;p&gt;When the same data is requested repeatedly, caching becomes our trusty time machine, transporting us back to a moment when the data was already prepared. Implementing cache at various levels—page, action, or fragment—allows us to serve data swiftly, reducing the load on our server and speeding up response times.&lt;/p&gt;

&lt;h3&gt;
  
  
  Technical Insight:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Use Rails' built-in caching mechanisms or integrate with Redis for more granular control.&lt;/li&gt;
&lt;li&gt;Cache scores and exercises, as they are the most dynamic elements in our data structure.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Act II: The Serializer – Crafting with Precision
&lt;/h2&gt;

&lt;p&gt;Just as a master craftsman selects the finest tools, we choose Oj for its speed and efficiency in shaping our JSON output. Oj is a gem that stands out for its performance, making it an ideal choice for our serialization needs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Technical Insight:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Add &lt;code&gt;gem 'oj'&lt;/code&gt; to your Gemfile.&lt;/li&gt;
&lt;li&gt;Replace &lt;code&gt;JSON.generate&lt;/code&gt; with &lt;code&gt;Oj.dump&lt;/code&gt; for faster serialization.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Act III: Eager Loading – The Art of Anticipation
&lt;/h2&gt;

&lt;p&gt;In the bustling kitchen of our application, eager loading is the sous-chef who prepares all the ingredients in advance. This technique prevents the common N+1 query problem, ensuring that our data is fetched in the most efficient manner possible.&lt;/p&gt;

&lt;h3&gt;
  
  
  Technical Insight:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;.includes&lt;/code&gt; with ActiveRecord to preload associated data.&lt;/li&gt;
&lt;li&gt;Example: &lt;code&gt;ClassRoom.includes(students: {lessons: {exercises: :scores}}).where(teacher_id: 1)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Act IV: Asynchronous Processing – The Ensemble of Background Tasks
&lt;/h2&gt;

&lt;p&gt;As in a symphony where each instrument plays independently yet harmoniously, asynchronous processing allows different parts of our data to be prepared in parallel. Sidekiq is our conductor, orchestrating these background tasks to perfection.&lt;/p&gt;

&lt;h3&gt;
  
  
  Technical Insight:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Set up Sidekiq to handle data serialization tasks outside the main request/response cycle.&lt;/li&gt;
&lt;li&gt;Create Sidekiq workers to serialize data and store it for subsequent requests.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Act V: Async Programming – The Power of Parallelism
&lt;/h2&gt;

&lt;p&gt;Rails' &lt;code&gt;load_async&lt;/code&gt;, in conjunction with &lt;code&gt;Concurrent::Async&lt;/code&gt;, is like having multiple artists painting different sections of our mural simultaneously. This parallelism ensures that our CPU is utilized to its fullest potential, speeding up the overall process.&lt;/p&gt;

&lt;h3&gt;
  
  
  Technical Insight:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Utilize &lt;code&gt;load_async&lt;/code&gt; for asynchronously loading ActiveRecord relations.&lt;/li&gt;
&lt;li&gt;Example: &lt;code&gt;ClassRoom.find(1).load_async.students.load_async.lessons&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Epilogue: The Road Ahead
&lt;/h2&gt;

&lt;p&gt;As we conclude our journey, we recognize that the path to optimization is a continuous exploration. The strategies we've discussed are but a few arrows in our quiver. As we venture further, we might discover new frameworks like Hanami, which offer built-in solutions for concurrency challenges.&lt;/p&gt;

&lt;h3&gt;
  
  
  Further Adventures:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Experiment with pagination to limit data depth and reduce payload size.&lt;/li&gt;
&lt;li&gt;Employ data compression techniques to enhance API response efficiency.&lt;/li&gt;
&lt;li&gt;Use monitoring and profiling tools to identify bottlenecks and refine performance.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By striking a balance between storytelling and technical guidance, we've crafted an article that not only educates but also engages. The art of serialization is a dance of efficiency and performance, and with the right steps, we can ensure that our data structures flow as smoothly as a well-written symphony.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>irbrc to help with your efficiency</title>
      <dc:creator>Lazy coder</dc:creator>
      <pubDate>Wed, 24 Jul 2024 19:16:35 +0000</pubDate>
      <link>https://dev.to/alazycoder101/irbrc-to-help-with-your-efficiency-1k2m</link>
      <guid>https://dev.to/alazycoder101/irbrc-to-help-with-your-efficiency-1k2m</guid>
      <description>&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
IRB.conf[:PROMPT][:DEV] = { # name of prompt mode
  :AUTO_INDENT =&amp;gt; false,          # disables auto-indent mode
  :PROMPT_I =&amp;gt;  "&amp;gt;&amp;gt; ",            # simple prompt
  :PROMPT_S =&amp;gt; nil,               # prompt for continuated strings
  :PROMPT_C =&amp;gt; nil,               # prompt for continuated statement
  :RETURN =&amp;gt; "    ==&amp;gt;%s\n"        # format to return value
}

#IRB.conf[:PROMPT_MODE] = :MY_PROMPT

# Tracking and Debugging Helpers

# Log method calls with their arguments
# Usage: track_method_calls(User, :save)
def track_method_calls(klass, method_name)
  klass.class_eval do
    old_method = instance_method(method_name)
    define_method(method_name) do |*args, &amp;amp;block|
      puts "Calling #{klass}##{method_name} with args: #{args.inspect}"
      old_method.bind(self).call(*args, &amp;amp;block)
    end
  end
end

# Track SQL queries
# Usage: track_sql_queries
def track_sql_queries
  ActiveSupport::Notifications.subscribe('sql.active_record') do |*args|
    event = ActiveSupport::Notifications::Event.new(*args)
    puts "SQL Query: #{event.payload[:sql]}"
    puts "Duration: #{event.duration.round(2)} ms"
    puts "---"
  end
end

# Count method calls
# Usage: count_method_calls(User, :update)
def count_method_calls(klass, method_name)
  count = 0
  klass.class_eval do
    old_method = instance_method(method_name)
    define_method(method_name) do |*args, &amp;amp;block|
      count += 1
      puts "#{klass}##{method_name} called #{count} times"
      old_method.bind(self).call(*args, &amp;amp;block)
    end
  end
end

# Track object allocations
# Usage: track_allocations { your_code_here }
def track_allocations
  require 'objspace'
  GC.disable
  before = ObjectSpace.count_objects
  yield
  after = ObjectSpace.count_objects
  puts "Object allocations:"
  after.each do |key, value|
    diff = value - before[key]
    puts "  #{key}: #{diff}" if diff != 0
  end
ensure
  GC.enable
end

# Profile code execution
# Usage: profile_code { your_code_here }
def profile_code
  require 'ruby-prof'
  RubyProf.start
  yield
  result = RubyProf.stop
  printer = RubyProf::FlatPrinter.new(result)
  printer.print(STDOUT)
end

# Track method execution time
# Usage: track_time(User, :expensive_method)
def track_time(klass, method_name)
  klass.class_eval do
    old_method = instance_method(method_name)
    define_method(method_name) do |*args, &amp;amp;block|
      start_time = Time.now
      result = old_method.bind(self).call(*args, &amp;amp;block)
      end_time = Time.now
      puts "#{klass}##{method_name} took #{(end_time - start_time) * 1000.0} ms"
      result
    end
  end
end

# Log instance variable changes
# Usage: track_ivar_changes(user, :name)
def track_ivar_changes(object, ivar_name)
  object.define_singleton_method("#{ivar_name}=") do |value|
    old_value = instance_variable_get("@#{ivar_name}")
    instance_variable_set("@#{ivar_name}", value)
    puts "#{object.class}##{ivar_name} changed from #{old_value.inspect} to #{value.inspect}"
  end
end


# Memory usage tracker
# Usage: track_memory { your_code_here }
def track_memory
  memory_before = `ps -o rss= -p #{Process.pid}`.to_i
  yield
  memory_after = `ps -o rss= -p #{Process.pid}`.to_i
  puts "Memory usage increased by #{memory_after - memory_before} KB"
end


def compare_methods(count = 1, *methods)
  require 'benchmark'
  Benchmark.bmbm do |b|
    methods.each do |method|
      b.report(method) { count.times { send(method) } }
    end
  end
  nil
end

def pp_hash(hash, indent=1)
  hash.each do |k, v|
    print "  " * indent
    if v.is_a?(Hash)
      puts "#{k}:"
      pp_hash(v, indent+1)
    else
      puts "#{k}: #{v}"
    end
  end
end

def measure_allocation
  before = GC.stat(:total_allocated_objects)
  yield
  after = GC.stat(:total_allocated_objects)
  puts "Allocated objects: #{after - before}"
end

def http_get(url)
  require 'httparty'
  response = HTTParty.get(url)
  puts "Status: #{response.code}"
  puts "Body: #{response.body}"
end

def explore_relation(relation)
  puts "SQL: #{relation.to_sql}"
  puts "Explained:"
  puts relation.explain
end

def parse_json(json_string)
  require 'json'
  JSON.pretty_generate(JSON.parse(json_string))
end

def list_constants(mod)
  mod.constants.sort.each do |const|
    puts "#{const} = #{mod.const_get(const)}"
  end
end

# Usage: class_hierarchy(Array)
def class_hierarchy(klass)
  hierarchy = [klass]
  while (klass = klass.superclass)
    hierarchy &amp;lt;&amp;lt; klass
  end
  hierarchy.each_with_index do |k, i|
    puts "#{' ' * i}#{k}"
  end
end

# Usage: method_info("hello", :upcase)
def method_info(obj, method_name)
  method = obj.method(method_name)
  puts "Name: #{method.name}"
  puts "Owner: #{method.owner}"
  puts "Parameters: #{method.parameters}"
  puts "Arity: #{method.arity}"
  puts "Source Location: #{method.source_location&amp;amp;.join(':')}"
end

# Usage: set_breakpoint(User, :save)
def set_breakpoint(klass, method_name)
  require 'pry'
  klass.send(:define_method, method_name) do |*args, &amp;amp;block|
    binding.pry
    super(*args, &amp;amp;block)
  end
end

# Usage: list_instance_variables(some_object)
def list_instance_variables(obj)
  obj.instance_variables.each do |var|
    puts "#{var}: #{obj.instance_variable_get(var).inspect}"
  end
end

# Usage: show_ancestors(Array)
def show_ancestors(klass)
  puts "Ancestors:"
  klass.ancestors.each { |ancestor| puts "  #{ancestor}" }
  puts "\nIncluded Modules:"
  (klass.ancestors - klass.superclass.ancestors - [klass]).each { |mod| puts "  #{mod}" }
end

# Usage: list_constants
def list_constants
  Module.constants.sort.each do |constant|
    puts constant
  end
end

# Usage: explore_methods("hello")
def explore_methods(obj)
  methods = {
    instance: obj.methods - Object.methods,
    inherited: obj.methods &amp;amp; Object.methods,
    singleton: obj.singleton_methods
  }

  methods.each do |category, method_list|
    puts "#{category.to_s.capitalize} methods:"
    method_list.sort.each { |method| puts "  #{method}" }
    puts
  end
end

# Usage: show_method_source(Array, :map)
def show_method_source(obj, method_name)
  require 'pry'
  obj.method(method_name).source.display
end

# Usage: list_classes
def list_classes
  Object.constants
    .select { |c| Object.const_get(c).is_a? Class }
    .sort
    .each { |klass| puts klass }
end

# Usage: method_location(Array, :map)
def method_location(obj, method_name)
  file, line = obj.method(method_name).source_location
  puts "Defined in #{file}:#{line}"
end

# Usage: subclasses_of(ActiveRecord::Base)
def subclasses_of(klass)
  ObjectSpace.each_object(Class).select { |k| k &amp;lt; klass }
end

def track_changes(target, method_name)
  original_value = target.send(method_name.to_s.sub('=', ''))
  puts "Starting to track changes to #{target}.#{method_name}"
  puts "Initial value: #{original_value.class}"

  trace = TracePoint.new(:line) do |tp|
    #next unless tp.method_id.to_s == method_name.to_s &amp;amp;&amp;amp; tp.self.instance_of?(target.class) &amp;amp;&amp;amp; tp.self == target
    next unless tp.self.instance_of?(target.class) &amp;amp;&amp;amp; tp.self == target &amp;amp;&amp;amp; tp.method_id.to_s.include?(method_name.to_s.sub('=', ''))

    current_value = target.send(method_name.to_s.sub('=', ''))
    if current_value != original_value
      puts "\nChange detected in #{target}.#{method_name}:"
      puts "  From: #{original_value.class}"
      puts "  To:   #{current_value.class}"
      puts "  At:   #{tp.path}:#{tp.lineno}"
      puts "  Backtrace:"
      puts caller[0..5].map { |line| "    #{line}" }
      puts '-'*60
      original_value = current_value
    end
  end

  trace.enable
  puts "Tracking enabled. Run 'trace.disable' to stop tracking."

  trace  # Return the trace object so it can be disabled later
end

def track_method(klass, method_name, process=nil)
  trace = TracePoint.new(:call) do |tp|
    next unless tp.method_id.to_s == method_name.to_s

    next unless (klass.is_a?(Class) ? tp.self.is_a?(klass) : tp.self == klass)

    puts '-'*20
    puts "#{tp.path}:#{tp.lineno}"
    puts "#{tp.defined_class}##{tp.method_id}" if tp.defined_class
    process.call if process
  end

  trace.enable
  if block_given?
    yield
    trace.disable
  else
    puts "Tracking enabled. Run 'trace.disable' to stop tracking."
  end

  trace  # Return the trace object so it can be disabled later
end

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>ruby</category>
      <category>irb</category>
    </item>
    <item>
      <title>N+1 in Ruby on Rails</title>
      <dc:creator>Lazy coder</dc:creator>
      <pubDate>Thu, 15 Jun 2023 22:52:14 +0000</pubDate>
      <link>https://dev.to/alazycoder101/n1-in-ruby-on-rails-42cm</link>
      <guid>https://dev.to/alazycoder101/n1-in-ruby-on-rails-42cm</guid>
      <description>&lt;h1&gt;
  
  
  How to detect N+1
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Prosopite
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Install
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gem &lt;span class="nb"&gt;install &lt;/span&gt;prosopite
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bundle add prosopite
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Gemfile&lt;/span&gt;
&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;'prosopite'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Configure
&lt;/h3&gt;

&lt;p&gt;Controller&lt;br&gt;
Add the following to &lt;code&gt;ApplicationController&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ApplicationController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActionController&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="k"&gt;unless&lt;/span&gt; &lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;production?&lt;/span&gt;
    &lt;span class="n"&gt;before_action&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="no"&gt;Prosopite&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;scan&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="n"&gt;after_action&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="no"&gt;Prosopite&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;finish&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Enable logging&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# config/environments/development.rb&lt;/span&gt;

&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;after_initialize&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="no"&gt;Prosopite&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rails_logger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;true&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Spec
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# config/environments/test.rb&lt;/span&gt;

&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;after_initialize&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="no"&gt;Prosopite&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rails_logger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;true&lt;/span&gt;
  &lt;span class="no"&gt;Prosopite&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;raise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;true&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# spec/spec_helper.rb&lt;/span&gt;

&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;before&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="no"&gt;Prosopite&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;scan&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;after&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="no"&gt;Prosopite&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;finish&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now everytime you run the test, you will be able to see any error about N+1 queries.&lt;/p&gt;

&lt;p&gt;Note: It won't raise any exception if only one association.&lt;/p&gt;

&lt;p&gt;For example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Teacher&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;
  &lt;span class="n"&gt;has_many&lt;/span&gt; &lt;span class="ss"&gt;:students&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;
  &lt;span class="n"&gt;belongs_to&lt;/span&gt; &lt;span class="ss"&gt;:teacher&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  No exception
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;teacher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Teacher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s1"&gt;'John'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;teacher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;students&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s1"&gt;'Tom'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="no"&gt;Teacher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;first&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;students&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;student&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;student&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;name&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Throw exception about N+1
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;teacher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Teacher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s1"&gt;'John'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;teacher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;students&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s1"&gt;'Tom'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;teacher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;students&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s1"&gt;'Jerry'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="no"&gt;Teacher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;first&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;students&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;student&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;student&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;name&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Prosopite::NPlusOneQueriesError:
       N+1 queries detected:
         SELECT &lt;span class="sb"&gt;`&lt;/span&gt;students&lt;span class="sb"&gt;`&lt;/span&gt;.&lt;span class="k"&gt;*&lt;/span&gt; FROM &lt;span class="sb"&gt;`&lt;/span&gt;students&lt;span class="sb"&gt;`&lt;/span&gt; WHERE &lt;span class="sb"&gt;`&lt;/span&gt;students&lt;span class="sb"&gt;`&lt;/span&gt;.&lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; ? LIMIT ?

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Convert object to Yaml by loading js lib</title>
      <dc:creator>Lazy coder</dc:creator>
      <pubDate>Thu, 15 Jun 2023 22:08:57 +0000</pubDate>
      <link>https://dev.to/alazycoder101/convert-object-to-yaml-by-loading-js-lib-4hn7</link>
      <guid>https://dev.to/alazycoder101/convert-object-to-yaml-by-loading-js-lib-4hn7</guid>
      <description>&lt;h2&gt;
  
  
  Convert object to Yaml
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getScript&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://unpkg.com/js-yaml@4.1.0/dist/js-yaml.min.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;jsyaml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dump&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myObject&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Convert object to JSON
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myObject&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>How to debug image build with Dockerfile</title>
      <dc:creator>Lazy coder</dc:creator>
      <pubDate>Thu, 15 Jun 2023 22:04:57 +0000</pubDate>
      <link>https://dev.to/alazycoder101/how-to-debug-image-build-with-dockerfile-51bh</link>
      <guid>https://dev.to/alazycoder101/how-to-debug-image-build-with-dockerfile-51bh</guid>
      <description>&lt;h1&gt;
  
  
  How to debug image build with Dockerfile
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Dockerfile to build the env for Ruby on Rails App
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; ruby&lt;/span&gt;
&lt;span class="c"&gt;# install essential libs/commands&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;apt-get update &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; apt-get &lt;span class="nb"&gt;install &lt;/span&gt;lib-ssl
...
# install gems
&lt;span class="k"&gt;RUN &lt;/span&gt;bundle &lt;span class="nb"&gt;install&lt;/span&gt;
...
# install npm
&lt;span class="k"&gt;RUN &lt;/span&gt;yarn &lt;span class="nb"&gt;install&lt;/span&gt;
COMMAND bundle exec rails server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;delete the following part causing errors
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM busybox
RUN echo 'hello world' &amp;gt; /tmp/test

RUN exit 1
....
RUN echo 'ready'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;del&gt;RUN exit 1&lt;/del&gt;&lt;br&gt;
Just remove &lt;code&gt;RUN exit 1&lt;/code&gt; and the following&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;run intermediate image with sha
Turn off buildkit to get the sha for each layer
&lt;code&gt;docker run --rm -it current sh&lt;/code&gt; to debug
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;❯ &lt;span class="nv"&gt;DOCKER_BUILDKIT&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;0 docker build &lt;span class="nt"&gt;-t&lt;/span&gt; &lt;span class="nb"&gt;test&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt;
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM busybox
 &lt;span class="nt"&gt;---&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; abaa813f94fd
Step 2/3 : RUN &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'hello world'&lt;/span&gt;
 &lt;span class="nt"&gt;---&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; Using cache
 &lt;span class="nt"&gt;---&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; 551ba8324834
Step 3/3 : RUN &lt;span class="nb"&gt;exit &lt;/span&gt;1
 &lt;span class="nt"&gt;---&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; Running &lt;span class="k"&gt;in &lt;/span&gt;5e3c523c38a3
The &lt;span class="nb"&gt;command&lt;/span&gt; &lt;span class="s1"&gt;'/bin/sh -c exit 1'&lt;/span&gt; returned a non-zero code: 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# pick sha of last successful layer&lt;/span&gt;
docker run &lt;span class="nt"&gt;--rm&lt;/span&gt; &lt;span class="nt"&gt;-it&lt;/span&gt; 551ba8324834 sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;nsenter&lt;/code&gt; to debug&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Enter into the &lt;a href="https://www.nginx.com/blog/what-are-namespaces-cgroups-how-do-they-work"&gt;name space&lt;/a&gt; of the process.&lt;br&gt;
Namespaces are a feature of the Linux kernel that partitions kernel resources such that one set of processes sees one set of resources while another set of processes sees a different set of resources.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;add &lt;code&gt;RUN sleep infinite&lt;/code&gt; to Dockerfile&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docker run -it --rm --privileged --pid=host justincormack/nsenter1&lt;/code&gt; to get to the building host &lt;sup id="fnref1"&gt;1&lt;/sup&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ps -ef|grep sleep&lt;/code&gt; to find pid&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;nsenter -p -m -u -i -n -t 10012 sh&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; busybox&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'hello world'&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;&lt;span class="nb"&gt;sleep &lt;/span&gt;infinite
&lt;span class="k"&gt;RUN &lt;/span&gt;&lt;span class="nb"&gt;exit &lt;/span&gt;1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;❯ docker build &lt;span class="nt"&gt;-t&lt;/span&gt; &lt;span class="nb"&gt;test&lt;/span&gt;  &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt;+] Building 10.7s &lt;span class="o"&gt;(&lt;/span&gt;5/7&lt;span class="o"&gt;)&lt;/span&gt;
 &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;internal] load build definition from Dockerfile                                                                                             0.0s
 &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; transferring dockerfile: 73B                                                                                                              0.0s
 &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;internal] load .dockerignore                                                                                                                0.0s
 &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; transferring context: 2B                                                                                                                  0.0s
 &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;internal] load metadata &lt;span class="k"&gt;for &lt;/span&gt;docker.io/library/busybox:latest                                                                                0.0s
 &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;1/4] FROM docker.io/library/busybox                                                                                                         0.0s
 &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; CACHED &lt;span class="o"&gt;[&lt;/span&gt;2/4] RUN &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'hello world'&lt;/span&gt;                                                                                                          0.0s
 &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;3/4] RUN &lt;span class="nb"&gt;sleep &lt;/span&gt;infinite
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;use &lt;code&gt;target&lt;/code&gt; to build sucessfully the image
&lt;a href="https://docs.docker.com/build/building/multi-stage/"&gt;multiple-stage builds&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM busybox as working
RUN echo 'hello world'

FROM working as error
RUN exit 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# build image with target&lt;/span&gt;
docker build &lt;span class="nt"&gt;-t&lt;/span&gt; &lt;span class="nb"&gt;test&lt;/span&gt; &lt;span class="nt"&gt;--target&lt;/span&gt; working &lt;span class="nb"&gt;.&lt;/span&gt;

&lt;span class="c"&gt;# run image to debug&lt;/span&gt;
docker run &lt;span class="nt"&gt;--rm&lt;/span&gt; &lt;span class="nt"&gt;-it&lt;/span&gt; &lt;span class="nb"&gt;test &lt;/span&gt;sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;








&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run &lt;span class="nt"&gt;--privileged&lt;/span&gt; &lt;span class="nt"&gt;--pid&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;host &lt;span class="nt"&gt;-it&lt;/span&gt; alpine &lt;span class="se"&gt;\&lt;/span&gt;
nsenter &lt;span class="nt"&gt;-t&lt;/span&gt; 1 &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="nt"&gt;-u&lt;/span&gt; &lt;span class="nt"&gt;-n&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;--rm : removes the container after it is stopped
-ti (or -t -i) : adds a tty and leaves the standard input opened&lt;/li&gt;
&lt;li&gt;--privileged : grants additional permissions to the container, it allows the container to gain access to the devices of the host (/dev)&lt;/li&gt;
&lt;li&gt;--pid=host : allows the containers to use the processus tree of the Docker host (the VM in which the Docker daemon is running)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  check pid
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;❯ docker run &lt;span class="nt"&gt;-ti&lt;/span&gt; &lt;span class="nt"&gt;--rm&lt;/span&gt; busybox sh
❯ docker ps
❯ docker inspect &lt;span class="nt"&gt;--format&lt;/span&gt; &lt;span class="s1"&gt;'{{.State.Pid}}'&lt;/span&gt; a57c56a83e54
18762
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;althernative way using alpine and nsenter command ↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
    </item>
  </channel>
</rss>
