Why is this map not working? - ruby-on-rails

I have a list of associado, and I want to select only the ones where eh_proprietario returns true. This is the mapping:
#possiveis_associados = associados.map { |e| e if e.eh_proprietario}
If I add a puts "#{e.eh_proprietario}" I can see it return true for two instances, but in my view, when I try to use this collection, I get an error because #possiveis_associados is nil.
<%= m.select :associado_id , options_from_collection_for_select(#possiveis_associados, :id, :razao_social), {include_blank: false}, {class: 'form-control'}%>
What am I doing wrong here?

You're looking for select, not map. Try
#possiveis_associados = associados.select { |e| e.eh_proprietario }
or, shorter
#possiveis_associados = associados.select(&:eh_proprietario)

Related

Values of radio_button_tag comes in a hash

Need data returned as
"results"=>[{"radio_result"=>"true"}, {"radio_result"=>"false"}...]
Currently have code:
#results.each_with_index do |result, index|
= radio_button_tag "results[][radio_result][#{index}]", true, result.radio_result?, id: "results__radio_result_#{index}_true"
= label_tag "results[][radio_result][#{index}][false]", 'Yes'
= radio_button_tag "results[][radio_result][#{index}]", false, !result.radio_result?, id: "results__radio_result_#{index}_false"
= label_tag "results[][radio_result][#{index}][true]", 'No'
It kinda works but returns data in wrong format ("results"=>[{"radio_result"=>{"0" => "true"}}, {"radio_result"=>{"0" => "false"}}...]
Changing code to = radio_button_tag "results[][radio_result](removing #{index} from it) should have fixed it - but instead - my labels are not showing up anymore
What happens if you move the index earlier?
= radio_button_tag "results[#{index}][radio_result]"
That being said, I can't help but feel like maybe there's a more Railsy way to accomplish this. I'm not sure off the top of my head, but maybe something like:
= f.fields_for result do |result_fields|
= result_fields.radio_button true, checked: result.radio_result?
= result_fields.radio_button false, checked: !result.radio_result?

How to push a blank name and a name on to this array in Rails

I have an array of names. I want the first option to be blank, and the last option to be "Joe".
Here is the code:
def index
#case_managers = client.personnel_search_by_client(current_client.client_id, nil, groupMnemonic: 'reimbursement_whitelist')
#case_managers_drop_down = {}
#case_managers.each do |case_manager|
#case_managers_drop_down[case_manager.name] = case_manager.to_json
end
end
In my views I have:
= form_tag work_lists_path, :method=> 'put' do |f|
.fieldset.field-group.field-group-inline.pull-left
.field.field-text.field-required
%label= t('workflow.duplicate_claim_manager')
= select_tag('case[case_manager]', options_for_select(#case_managers_drop_down, #selected_case_manager))
This correctly gets all the names. I thought before the array I would do something like #case_managers_drop_down.push(" ") to get a blank option and then likewise for Joe. But this doesnt seem to be working. Any idea on how I can append to this array?
To get a blank option at first place in select_tag use include_blank boolean attribute like :
= select_tag('case[case_manager]', options_for_select(#case_managers_drop_down, #selected_case_manager), :include_blank => true)
OR use prompt like :
= select_tag('case[case_manager]', options_for_select(#case_managers_drop_down, #selected_case_manager), :prompt => "Please select")
For last option as joe do it like :
def index
#case_managers = client.personnel_search_by_client(current_client.client_id, nil, groupMnemonic: 'reimbursement_whitelist')
#case_managers_drop_down = {}
#case_managers.each do |case_manager|
#case_managers_drop_down[case_manager.name] = case_manager.to_json
end
#case_managers_drop_down["joe"] = ""
end
Hopefully it will work!

making hash to produce JSON

I am creating a hash which would produce JSON to be used in APIs.
To make Hash I am trying this:
presses = Press.select(:id, :name)
pulls = Pull.select(:id, :name)
response = {list: {presses: presses, pulls: pulls}}
Actually works good and produces:
{"list":{
"presses":[
{"id":1,"name":"p40"},
{"id":2,"name":"p41"}],
"pulls":[
{"id":1,"name":"Best Effort"},
{"id":2,"name":"First Good"}]
}}
But the requirement has changed to make it like:
{"list":{
"presses":[
{"id":1,"name":"p40"
"pulls":[
{"id":1,"name":"Best Effort"},
{"id":2,"name":"First Good"}]},
{"id":2,"name":"p41"
"pulls":[
{"id":1,"name":"Best Effort"},
{"id":2,"name":"First Good"}]},
],
}}
Note Each press object should contain all pull object. So far don't know how to achieve this. Any Idea?
I think this is Rails question. Thus I have #attributes method. Here is my attempt
presses = Press.select(:id, :name)
pulls = Pull.select(:id, :name)
response = { list: {
presses: presses.map do |press|
press.attributes.merge(pulls: pulls.map(&:attributes))
end
}
}.to_json
This will do what you want
pulls = Pull.select(:id, :name)
presses = Press.select(:id, :name)
presses = presses.collect do |press|
press.merge({pulls: pulls})
end
response = {list: {presses: presses}}

Ruby simple map object to has not working

I have a simple problem but I cannot find a solution.
I have a Forum model (active record) with several fields.
I'm creating a class method that return to me an has with one value as key (not the id) and the other as value.
This is my method:
Forum.all.map { |f| [f.old_id => f.icon.url(:micro) ]}
It returns
[[{10=>"/images/fallback/icon_fallback.png"}],
[{6=>"/images/fallback/icon_fallback.png"}],
[{18=>"/images/fallback/icon_fallback.png"}]]
instead of
{10=>"/images/fallback/icon_fallback.png", 6=>"/images/fallback/icon_fallback.png", 18=>"/images/fallback/icon_fallback.png"}
What is the error?
In your code, map returns an array and the square brackets produce arrays containing hashes.
res = {}
Forum.all{|f| res[f.old_id] = f.icon.url(:micro) }
in short you can just modify like this, change square brackets to curly brackets:
Forum.all.inject({}) { |r,f| r.merge!(f.old_id => f.icon.url(:micro)) }
You can make a minimal change to your code and receive your needed result by using to_h:
Forum.all.map { |f| [f.old_id, f.icon.url(:micro)] }.to_h
Yes, you can use reduce or inject method, or just construct the Hash from Arrays:
Hash[Forum.all.map { |f| [f.old_id, f.icon.url(:micro) ]}]
for ruby-2.0 you can use #to_h method:
Forum.all.map { |f| [f.old_id, f.icon.url(:micro) ]}.to_h
use active supports each_with_object:
Forum.all.each_with_object({}) { |f, h| h[f.old_id] = f.icon.url(:micro) }

adding a key value to array isn't display the key value on debugger

ruby on rails
def get_process_order_items
po = ProcessOrder.find(params[:id])
ids = po.process_order_items.map { |e| e.fabric.id }
a = Fabric.where(:id => ids.uniq())
a.collect{ |b| b[:pre_weight] = get_fabric_num(params[:id]) }
debugger
wando_grid_for_array(params, a)
end
can't display the key value on a. on debugger. Could you tell me?
thanks
a.collect{ |b| b[:pre_weight] = get_fabric_num(params[:id]) } seems to have no side-effect, did you mean a.collect!{ |b| b[:pre_weight] = get_fabric_num(params[:id]) }, perhaps?

Resources