jeudi 31 mars 2022

String mongo query run on rails

When i ran string mongo query on rails simple code { find: {status: 1}, order: {status: 1}, limit: 10} like this working fine with eval function but this code not working eval on eval
"{ find : {status: 10, $expr: {$ne: ['$published_task_count', '$finished_task_count']}}}"

any solution run string mongo query on rails

How can fix n+1 issue using associated values in rails?

I have those 3 tables in a relatiohship and i'm trying to speed up performance.

3 models (transformer, customer, customer_details )

Table transformers (id, name, customer_id). Table customers (id, name)- Table customer_details (id, name,customer_id)

Transformer.rb
 belongs_to :customer

Customer.rb
  has_many :transformers
  has_many :customer_details

CustomerDetail.rb
  belongs_to :customer

Here is the controller

@customers = Customer.include(:customer,:customer_detail).all

Here is the view

<% @transformers.each do |transformer| %>
  <%= transformer.name %>
  <%= transformer.customer.name %>
  <%= transformer.customer.customer_detail.name %>
<% end %>

Here is the log displayed:

  ↳ app/views/transformers/partials/_table.html.erb:20
  Customer Load (0.2ms)  SELECT "customers".* FROM "customers" WHERE "customers"."id" IN (?, ?, ?)  [["id", 1], ["id", 2], ["id", 3]]
  ↳ app/views/transformers/partials/_table.html.erb:20
  CustomerDetail Load (0.2ms)  SELECT "customer_details".* FROM "customer_details" WHERE "customer_details"."id" IN (?, ?, ?)  [["id", 1], ["id", 2], ["id", 3]]
  ↳ app/views/transformers/partials/_table.html.erb:20
  CustomerDetail Load (0.2ms)  SELECT "customer_details".* FROM "customer_details" WHERE "customer_details"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  ↳ app/views/transformers/partials/_table.html.erb:27
  CustomerDetail Load (0.2ms)  SELECT "customer_details".* FROM "customer_details" WHERE "customer_details"."id" = ? LIMIT ?  [["id", 2], ["LIMIT", 1]]
  ↳ app/views/transformers/partials/_table.html.erb:27
  CustomerDetail Load (0.1ms)  SELECT "customer_details".* FROM "customer_details" WHERE "customer_details"."id" = ? LIMIT ?  [["id", 3], ["LIMIT", 1]]

The result should

  ↳ app/views/transformers/partials/_table.html.erb:20
  Customer Load (0.2ms)  SELECT "customers".* FROM "customers" WHERE "customers"."id" IN (?, ?, ?)  [["id", 1], ["id", 2], ["id", 3]]
  ↳ app/views/transformers/partials/_table.html.erb:20
  CustomerDetail Load (0.2ms)  SELECT "customer_details".* FROM "customer_details" WHERE "customer_details"."id" IN (?, ?, ?)  [["id", 1], ["id", 2], ["id", 3]]

I tried but is a wrong code.

@customers = Customer.include(:customer,:customer_details).all

mardi 22 mars 2022

Timepicker in Rails

I have included this in the .html.slim file

= f.input :customized_agent_timeslot 
.container
  .row
    .col-sm-2
      .form-group
        #datetimepicker3.input-group.date
          input.form-control type="text" /
          span.input-group-addon
            span.glyphicon.glyphicon-time
    javascript:
      | $(function () {
          | $('#datetimepicker3').datetimepicker({
              | format: 'H:i'
          | });
      | });

Added this in the application.css.sass

 //= require datetimepicker

Added this in the application.js.coffee

 #= require datetimepicker

in the gem file i have included

   gem 'rails-assets-datetimepicker'

i am able to see the UI but on clicking the dropdown for choosing time doesn't show.

Are there any steps that i 'm missing up??

vendredi 18 mars 2022

ArgumentError: invalid date in ROR

Hello i finding average of array of dates. while running the code i am getting error as--> ArgumentError: invalid date in ROR

This is how my code looks like ---->

 arrays = [Tue, 08 Dec 2020 20:21:03 IST +05:30, Tue, 08 Dec 2020 20:21:05 IST +05:30]  # this is the date format i am getting from the database
 i = 0
  arrays.each do |array|   # arrays get value as array of dates 
    array1[i] = Date.parse(array.strftime('%Y-%d-%m')) # i am getting the error in this line 
    i = i+1
  end

mercredi 16 mars 2022

redmine - install a second instance with different ruby version

i tried to create a second redmine instance on one server, but also with a different ruby version. I firstly switched to 2.6 ruby version with rvm, created a second DB, a second user and of cause a second redmine folder in my opt directory.

Then i proceeded with the same steps of the installation as in the first one.

When i finished the installation, the information page of the second redmine instance told me that neither data or pluginassets were writable, also the ruby version was the same as with the first installation (2.7).

Is there any part of the docs i dont know which tells you on how to correctly install a second redmine instance? I think in this one i messed up Thank you so much for any reply

mardi 8 mars 2022

Como puedo hacer para cuando presione el boton de Submit en mi DualListBox todos los elementos seleccionados cambien su Invoice_status a "Facturado"? [closed]

Investigue y parece que tengo que hacer una function con js algo como submit algo como

$('#invoice_status').on('submit', function() {
($(this).val() == 'Facturado')

Pero de ahi a mas alla no tengo ni la menor idea de como hacerlo

Codigo

  <div class="">
                <%= form.select :sell_invoice,
                  options_for_select(
                  @company.sell_invoices.where(invoice_status: 1).map do |i|+i.correlative+' | '+i.rut+' | '+ unless i.business_name.length <= 25
                                                                       i.business_name.first(25)+ "..."
                                                                       else
                                                                       i.business_name
                                                                     end   +' |  '+i.document_date.to_s+' | '+i.document_type+' | '+i.source+' | '+number_to_currency_cl(i.total,0)+' | '+i.payed_amount.round.to_s+' | '+i.due_date.to_s+' | '+i.invoice_status+' | '+ number_to_currency_cl(i.lack_pay,0)+' | '+i.money+' | '+i.user.name
                                                                   end
                                                                   ),

                  {},
                  {multiple: true, class: "dual",} %>
              <div class="actions">
                  <%= form.submit 'Facturar',style:"background-color:#1ab394;color:white;margin-top:10px;" , class:"btn btn-md btn-block" %>
                <% end %>
                </div>

dimanche 6 mars 2022

Ruby class accessing protected method of child class?

I have a background in Node Js and I am new to Ruby, Pardon me If this question is a noob. I have 3 classes like this.

class Babylon::Metric::Counter < Babylon::Metric
  protected

  def record
    # some code here
  end
end
class Babylon::Metric::Gauge < Babylon::Metric
  protected

  def record
    # some code here
  end
end

and the parent class of these is

class Babylon::Metric
  class ArgumentError < ArgumentError; end

  def initialize name, category, amount
    @name = name
    @category = category
    @amount = amount
  end

  def self.call name:, category:, amount:
    new(name, category, amount).()
  end

  # What is it doing here exactly is it calling Counter or Gauge record method from here, 
  # based on valid method returns true or false?

  def call
    valid? ? record : notify
  rescue => e
    begin
      raise Babylon::Error, e.inspect
    rescue Babylon::Error => e
      Babylon::Event.(event: "babylon.error", error: e)
    end
  end

  def valid?
    # do validation
  end

  def notify
    # do some stuff 
  end

end

I think that the call method is able to in turn call Counter class and Gauge class record methods If the valid method returns true, But I don't know how it can call as these are protected members ?.

samedi 5 mars 2022

Ruby class accessing protected member of child class?

I have a background in Node Js and I am new to Ruby, Pardon me If this question is a noob. I have 3 classes like this.

class Babylon::Metric::Counter < Babylon::Metric
  protected

  def record
    # some code here
  end
end
class Babylon::Metric::Gauge < Babylon::Metric
  protected

  def record
    # some code here
  end
end

and the parent class of these is

class Babylon::Metric
  class ArgumentError < ArgumentError; end

  def initialize name, category, amount
    @name = name
    @category = category
    @amount = amount
  end

  def self.call name:, category:, amount:
    new(name, category, amount).()
  end

  # What is it doing here exactly is it calling Counter or Gauge record method from here, 
  # based on valid method returns true or false?

  def call
    valid? ? record : notify
  rescue => e
    begin
      raise Babylon::Error, e.inspect
    rescue Babylon::Error => e
      Babylon::Event.(event: "babylon.error", error: e)
    end
  end

  def valid?
    # do validation
  end

  def notify
    # do some stuff 
  end

end

I think that the call method is able to in turn call Counter class and Gauge class record methods If the valid method returns true, But I don't know how it can call as these are protected members ?.

mercredi 2 mars 2022

Does rails have async controller?

I am trying to write in a way similar to node.js. I would like to use async libraries like concurrent-ruby. The method DoTaskAsync returns a future and I could chain with a then function. The main thread of the calls the original post method could return quickly without being blocked.

class TasksController < ApplicationController
  def post
    DoTaskAsync
    .then do |res|
      respond_to do |format| # possible?
        format.json { render json: res }
      end
    end
  end
end