I have this situation:
skeleton =
{
"timeline" =>
{
"data" => []
}
}
template =
{
"A" => "",
"B" => "",
"C" => "",
}
From the controller I make a query which returns me an array of hashes:
#cdr = Cdr.select("start, end, clid")
then I iterate over the array and set the "template" hash fields and in the last step I append this hash to an array which belongs to "skeleton" hash:
#cdr.each do |cdr|
template["A"] = cdr.start
template["B"] = cdr.end
template["C"] = cdr.clid
skeleton["timeline"]["data"] << template
end
so to expected result is:
skeleton =
{
"timeline" =>
{
"data" => [
{
"A" => "sample1",
"B" => "sample2",
"C" => "sample3",
},
{
"A" => "sample4",
"B" => "sample5",
"C" => "sample6",
}
]
}
}
but the real result I'm getting is:
skeleton =
{
"timeline" =>
{
"data" => [
{
"A" => "sample1",
"B" => "sample2",
"C" => "sample3",
},
{
"A" => "sample1",
"B" => "sample2",
"C" => "sample3",
}
]
}
}
all entries in the array contain same data. Why?
Try creating a new template array through each cycle through. I don't think you can change the value of the key while it is being used as a key.
#cdr.each do |cdr|
temp_inst = template.clone
temp_inst["A"] = cdr.start
temp_inst["B"] = cdr.end
temp_inst["C"] = cdr.clid
skeleton["timeline"]["data"] << temp_inst
end
Related
I have two collections of hashes
and_filters = [{:filter=>:brand, :value=>"Fila"}, {:filter=>:brand, :value=>"Adidas"}]
or_filters = [{:filter=>:gender, :value=>"Hombre"}]
and i need make like the following struct
:_or => [
{ :_and => [
{:gender => "Hombre"},
{:brand => "Adidas"}]
},
{ :_and => [
{:gender=>"Hombre"},
{:brand=>"Fila"}]
}
]
For this i did
query[:_or] = []
or_filters.each do |or_f|
query[:_or] << {
:_and => [
and_filters.map do |and_f|
{and_f[:filter] => and_f[:value]}
end
{ or_f[:filter] => or_f[:value] }
]
}
end
but an error Expected: { shows in code. Apparently the second loop is badly syntactically
It's not pretty, but I believe this gives the desired results:
{_or: or_filters.each_with_object([]) do |or_filter, or_filter_ary|
or_filter_hsh = {or_filter[:filter] => or_filter[:value]}
and_filters.each do |and_filter|
and_filter_hsh = {and_filter[:filter] => and_filter[:value]}
or_filter_ary << {_and: [or_filter_hsh, and_filter_hsh]}
end
end
}
Which gives:
{:_or => [
{ :_and => [
{:gender=>"Hombre"},
{:brand=>"Fila"}
]},
{ :_and => [
{:gender=>"Hombre"},
{:brand=>"Adidas"}
]}
]}
It looks like you want every combination of the given and_filters with the given or_filters. In that case, and assuming you don't care about order (:gender before :brand vs. the other way around) Array#product is your friend:
result = {
_or: and_filters.product(or_filters).map do |a|
{ _and: a.map {|filter:, value:| { filter => value }} }
end
}
# => {
# :_or => [
# {:_and => [{:brand=>"Fila"}, {:gender=>"Hombre"}]},
# {:_and => [{:brand=>"Adidas"}, {:gender => "Hombre"}]}
# ]
# }
See it in action on repl.it: https://repl.it/#jrunning/HorizontalDirectCharmap
Thats what i was looking for
query = {}
query[:_or] = or_filters.map do |or_f|
and_filters_aux = and_filters.dup
and_filters_aux << or_f
{ :_and => and_filters_aux.map{|hsh| {hsh[:filter] => hsh[:value]} } }
end
https://repl.it/repls/ShyLateClients
Say I have controller params with the following structure:
{
"foo" => {
"id" => 123,
"children" => {
"0" => {
"a" => "a"
},
"1" => {
"b" => "b"
}
}
}
}
How can I permit all the data explicitly? I don't want to permit arbitrary data at any point in the hierarchy.
I had expected this work:
params.require(:foo).permit(:id, children: { "0" => [:a], "1" => [:b] })
However, it returns:
{ "id" => 123, "children" => { "0" => {}, "1" => {} } }
How can I whitelist the permitted attributes for each child?
Try square brackets instead of braces:
params.require(:foo).permit(
:id,
children: [
"0": [:a],
"1": [:b]
]
)
Try this
params.require(:foo).permit(:id, :children => { :"0" => [:a], :"1" => [:b] })
I have this:
produtos = LineItem.select('codigosku, quantity').where("cart_id = #{session[:cart_id] } ")
I need to insert the result of this select (produto variable), here:
message = Hash.new
message = {
"tem:carrinho" => {"gpa:CEP" => params[:cep],
"gpa:CNPJ" => 'doc',
"gpa:IdCampanha" => 1111,
"gpa:Produtos" => {"gpa:DadosListaProdutoCarrinhoDTO" =>
{
HERE! VALUES OF "PRODUTOS VARIABLE"
}
}
}
}
How can I do this?
Thanks in advance!
create your array:
line_items_array = line_items.map{|li| li.attributes }
Then insert the array within your hash.
like in apneadiving example, use map to create an array from the produtos data; use attributes to return all data (it is a hash) from your selected data
message = {
"tem:carrinho" => {
"gpa:CEP" => params[:cep],
"gpa:CNPJ" => 'doc',
"gpa:IdCampanha" => 1111,
"gpa:Produtos" => {
"gpa:DadosListaProdutoCarrinhoDTO" => produtos.map { |item| item.attributes }
}
}
}
or if you need to be more specific about the keys in the produtos and append it after initialization
# initialize the Produtos to nil
message = {
"tem:carrinho" => {
"gpa:CEP" => params[:cep],
"gpa:CNPJ" => 'doc',
"gpa:IdCampanha" => 1111,
"gpa:Produtos" => nil
}
}
# build an array of DadosListaProdutoCarrinhoDTO
list = produtos.map do |item|
{
"gpa:DadosListaProdutoCarrinhoDTO" => {
"codigosku" => item.codigosku,
"quantity" => item.quantity
}
}
end
# set the Produtos key to an array of DadosListaProdutoCarrinhoDTO
message["tem:carrinho"].merge!({ "gpa:Produtos" => list })
Controller functions receivent parameters like
{"v1" => { "v2" => "1", "v3" => "" }, "v4" => "true"}
The params function allows to use
x = params[:v1], equivalent to x = params["v1"]
if params[:v4], equivalent to ["true", "1"].include?(params["v4"])
if (params[:v1][:v2] == 1), equivalent to params["v1"]["v2"] == "1"
Is there any method to have the behavior than params function, but with other datas ?
I want to be able to write something like that...
my_params = {"v1" => { "v2" => "1", "v3" => "" }, "v4" => "true"}
x = my_params[:v1]
if my_params[:v4]
if (my_params[:v1][:v2] == 1)
Or with a function some_function
x = some_function(my_params)[:v1]
if some_function(my_params)[:v4]
if some_function(my_params)[:v1][:v2] == 1)
I'm in Rails 2.
You want a hash with indifferent access:
h = { a: { b: 1, 'c' => 2 } }
=> {:a=>{:b=>1, "c"=>2}}
h[:a][:c]
=> nil
h2 = h.with_indifferent_access
=> {"a"=>{"b"=>1, "c"=>2}}
h2['a'][:c]
=> 2
h2[:a][:c]
=> 2
I am using Ruby on Rails 3 and I would like to "trasform" the following array so that I can use my custom logic to access its data.
This is the original array from which I have to build a new one
[
{
"account" => {
"id" => 45,
"name" => "Test_name",
"..." => ..."
}
},
{
"other" => {
"sub_other" => {...}
}
}
]
I would like to trasform the above array so that I can do in my controller something like
array_name[45]
# => {
"name" => "Test_name",
"..." => ..."
}
but only for the account hashs. The other hash should remain untouched.
How can I proceed to build the new array?
If I understand your requirements correctly, I think you are better off constructing a hash from account id to account data. Perhaps something like this will work:
arr = [
{
"account" => {
"id" => 45,
"name" => "Test_name",
"..." => "..."
}
},
{
"other" => {
"sub_other" => "..."
}
}
]
account_hashes = arr.select {|item| item.keys.first == "account"}
answer = account_hashes.inject({}) do |acc, item|
acc[item["account"].delete("id")] = item["account"]
acc
end