Sri Sri - 2 months ago 5
Javascript Question

Select null record through the query string params?

I have the following code in my javascript file which is filtering the records from my table in rails.

var dict = {
'columns' : {
'Sun': 5,
'Rain': [1,2,3]
}
}


which returns

"columns%5BSun%5D=5&columns%5BRain%5D%5B%5D=1&columns%5BRain%5D%5B%5D=2&columns%5BRain%5D%5B%5D=3"


The above code works however it missing a record where the
Rain is null
.

Is there anyway that I can get that
null
record like the following in javascript?

var dict = {
'columns' : {
'Sun': 5,
'Rain': [1,2,3,null]
}
}


which returns

"columns%5BSun%5D=5&columns%5BRain%5D%5B%5D=1&columns%5BRain%5D%5B%5D=2&columns%5BRain%5D%5B%5D=3&columns%5BRain%5D%5B3%5D="


The above code is not working.

I want to pass 'null' to Rails to get all
NULL
records back from
ActiveRecord
.

Thanks in advance.

Answer

I hope this is what you want

columns = params[:columns]
rain = columns[:Rain]
rain.each do |r|
    if r.empty?
        YourModel.where(:field => nil) # this query can differ
    else
        YourModel.where(:field => r.to_i) # this query can differ
    end
end
Comments