Skip to content

Async Barrier for multiple ActiveRecord queries #265

@nguyenhothanhtam0709

Description

@nguyenhothanhtam0709

I'm learning about using Rails with Falcon web server. I want to wait for multiple ActiveRecord queries (something like await Promise.all of Javascript). Does the below code do the same effect?

def index
    task_query = Task
    if @search.present?
      task_query = task_query.where('title LIKE ?', "%#{@search}%")
    end

    barrier = Async::Barrier.new
    Async do
      barrier.async do
        @tasks = task_query.select(:id, :title, :description, :created_at, :updated_at)
                           .limit(@per_page)
                           .offset(@offset)
      end
      barrier.async do
        @count = task_query.count(:id)
      end

      begin
        barrier.wait
      ensure
        barrier.stop
      end
    end
  end

Please correct me if I'm wrong. Thank you so much.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions