0

I have an array of hashes.. some field values are blank ie value => ""

When I try to save them in db I get this error -- Column count doesn't match value count at row 1 (Mysql::Error) --which is basically when columns and value dont match...

I cannot try to match the column and values..... for some the data might be empty and for others values might be there..

So the best way seems to be to replace empty value ie value => "" into putting a "-" there ie value => "-"

I want to do change this {"value"=>"", "field"=>"x"} to
{"value"=>"-", "field"=>"x"}

In Ruby

I have tried the below options of alex and dan - but I am still getting errors.. might be this can help... the output I have is like

data = {"title"=>"Book1set", "id"=>"4899364", "columns"=>[{"name"=>"", "value"=>"3.85", "field"=>"price", "sort_order"=>""}, {"name"=>"", "value"=>"1.14", "field"=>"tax", "sort_order"=>""}]} {"title"=>"Book2set", "id"=>"4899364", "columns"=>[{"name"=>"", "value"=>"3.85", "field"=>"price", "sort_order"=>""}, {"name"=>"", "value"=>"1.14", "field"=>"tax", "sort_order"=>""}]} ...... (1000 odd title book set series like this..)

and I extract the field and value and save in db...

and in some of it is like this... "value"=>""

which is what I am trying to replace by "value"=>"-"

3
  • 1
    I'd suggest updating the MySQL schema to allow NULL values for the column: ALTER TABLE table MODIFY column VARCHAR(255); (or whatever column type you want, as long as it does not have NOT NULL). Commented Sep 11, 2014 at 22:42
  • 2
    Arbitrarily changing a field to allow nulls isn't good practice unless nulls are actually valid and have been accounted for. Doing that without understanding why can lead to a real messed up table/database. It's better to analyze the code, figure out why nils occur and whether they should be nil, and then decide whether the DB should allow them. Commented Sep 11, 2014 at 22:59
  • I tried that - but it didn't solve the issue.. also as said above its not a good practice to do that.. Commented Sep 12, 2014 at 9:20

2 Answers 2

1

Here is a similar but shorter way to achieve the same result

h = {"value"=>"", "field"=>"x"}
Hash[h.to_a.map {|k, v| [k, v.empty? ? "-" : v] }]
Sign up to request clarification or add additional context in comments.

1 Comment

Tried this.. had a error on to_a.map not recognized.. seems in ruby 1.9 and above it is not and has to be replaced with lines.to_a.map - but as such v.empty is a better way to search for
0
puts [ {"value"=>"", "field"=>"x"} ].map { |h|
  h.each_key { |k| h[k] = "-" if h[k].length == 0 }
}.inspect

Example output:

$ ruby ./t.rb  
[{"value"=>"-", "field"=>"x"}]

1 Comment

It checks keys's whose length is 0 and then replaces it... technically v.empty is a in build function... and a better option i think

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.