1

the following query return as follow:

@cref = Creference.all
@cref = @cref.group_by{|cc| cc["name"]}

result: Object {US: Array[1], UK: Array[1]}

And these query return the result as:

@countries = Product.joins(:user).distinct.where("state is not null and country <> ''").where(:users => {:merchant_status => 1}).pluck(:country)
@countries.map! {|country| country.split.map(&:capitalize).join(' ')}
@countries = @countries.uniq

result: ["US", "UK"]

I am using gon to pass the result to the country drop down list:

gon.search_location_list = @countries +  @cref

How can I make the first result format to match the second one? Thanks!!!

2
  • 3
    hey you can use as @countries + @cref.keys Commented Apr 5, 2016 at 6:28
  • Vishal is right. This is the best solution. Commented Apr 5, 2016 at 6:30

3 Answers 3

1

How can I make the first result format to match the second one?

In your first example you are using group_by which returns a Hash. In second example you are using map which returns an Array. Use the same thing both places to get similar format.

@cref = @cref.map{|cc| cc["name"]}
# ["US", "UK"]
Sign up to request clarification or add additional context in comments.

Comments

0

After your first two lines, add this line:

@cref = @cref.values

Comments

0

In short you question is that you want to concatenate array values with hash keys. So for this the simplest solution is

@cref = @cref.group_by{|cc| cc["name"]}.keys

This would return the keys in array format and than you can concatenate. But that would result is same value but you can call uniq() on it.

Comments

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.