---
title: "Generate a BARCODE for a Product"
author: "Yaroslav Shmarov"
date: "2021-09-24"
canonical_url: "https://blog.superails.com/ruby-on-rails-generate-barcodes"
markdown_url: "https://blog.superails.com/ruby-on-rails-generate-barcodes.md"
description: "Imagine if you could generate a barcode for each of your products... Well, that's easy! The previous post{:target=\"blank\"} focused on QR codes."
tags: ["ruby","rails","ruby-on-rails","active-storage","barcode","service-objects"]
license: "GNU AGPL v3. Credit Yaroslav Shmarov and link the canonical URL."
video_url: "https://www.youtube.com/watch?v=2AG-JR_zbSU"
---

# Generate a BARCODE for a Product

Imagine if you could generate a barcode for each of your products... Well, that's easy!

[The previous post](https://blog.superails.com/qr-code-generation-active-storage-service-objects){:target="blank"}
focused on QR codes.

Final result with both Barcodes and QR for each product:

![barcode and qr in a rails app](https://blog.superails.com/assets/images/barcode-and-qr.png)

**There difference between generating QRcodes and BARcodes is very minor.**

But for Barcodes we will use another gem - [https://github.com/toretore/barby](https://github.com/toretore/barby){:target="blank"}.

We generated QR codes for the product URL. We will generate Barcodes for the product name.

### Installation

console
```ruby
bundle add barby
bundle add chunky_png
```

app/models/product.rb
```ruby
  has_one_attached :barcode

  after_create :generate_code
  def generate_code
    GenerateBarcodeService.new(self).call
  end
```

app/services/generate_barcode_service.rb
```ruby
class GenerateBarcodeService
  attr_reader :product

  def initialize(product)
    @product = product
  end

  require 'barby'
  require 'barby/barcode/code_128'
  require 'barby/outputter/ascii_outputter'
  require 'barby/outputter/png_outputter'

  def call
    barcode = Barby::Code128B.new(product.title)

    # chunky_png required for THIS action
    png = Barby::PngOutputter.new(barcode).to_png

    image_name = SecureRandom.hex

    IO.binwrite("tmp/#{image_name}.png", png.to_s)

    blob = ActiveStorage::Blob.create_after_upload!(
      io: File.open("tmp/#{image_name}.png"),
      filename: image_name,
      content_type: 'png'
    )

    product.barcode.attach(blob)
  end
end
```
