Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

269
Visualizações
How can I replace null value with n/a in ruby on rails?

I am working on dynamic chart created through chartkick and chart.js in rails.

data shown from console that consists of null data.

The data got null is not the problem. The problem is that I want to display null/empty to n/a.

My chart looks like:

chart

To solve this, I first update the database through the code mention below:

Roster.where(caste_group: "").update_all(caste_group:"n/a")

But this is exactly not what I wanted. Everytime updating the database is not efficient. Then I used transform key helper. The code goes as

    caste_group = Roster.group(:caste_group).count
   @caste_group = caste_group.transform_keys{ |key| key==""? 
   "n/a":key }

Here Roster is the model which consist of following attribute caste_group as string and user select caste_group as dropdown. Using this transform key helper I got the result which doesn't display null value. null/empty value isn't rename to n/a. And my code in a view goes as

 = line_chart @event_chart,discrete:true, legend: "bottom",curve: false,
           colors: ["#67b55e","#d43766","#729be0"]
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

I think you can do this at many levels, so depends on what you need. One way to solve this would be to set a default value in the database. So every time your model is saved with null it will actually be saved as "n\a".

You can run a migration like so:

change_column_default :rosters, :caste_group, "n\a"

But maybe you don't want to edit all the data in the DB. In that case, you can do it at the model or presenter level. I think the code you posted from the helper is all right. Are you not satisfied with it?

over 4 years ago · Santiago Trujillo Relatório

0

If your query can return null, and "", e.g

Roster.group(:caste_group).count
{
  nil=>12,
  ''=>123,
  'name_first'=>2
}

This can help

sql = <<-SQL
  SELECT
    CASE
    WHEN caste_group IS NULL
    THEN 'n/a'
    WHEN caste_group=''
    THEN 'n/a'
    ELSE caste_group
    END AS caste_group,
    count(*)
  FROM rosters
  GROUP BY 1
SQL
result = ActiveRecord::Base.connection.execute(sql).to_a

This will compile NULL or '' to 'n/a'.

example result:

[
  {"caste_group"=>"n/a", "count"=>"1"},
  {"caste_group"=>"name_first", "count"=>"2"}
]

Next you can transform this in needed hash.

result.each_with_object({}) { |caste_group, res| res[caste_group['caste_group']] = caste_group['count'] }

e.g

{"n/a"=>"1", "name_first"=>"2"}
over 4 years ago · Santiago Trujillo Relatório

0

I thankfully solves this. The problem with mine is that I have already updated the database to "n/a". So, when I change the key through transform_keys I am facing the problem. After that I rename n/a to "N/A" and solves for me.

   caste_group = Roster.group(:caste_group).count
@caste_group = caste_group.transform_keys{ |key| key==""? "N/A":key }

I am posting the answer because it might be helpful for others in future.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda