Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

263
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!