1

I need to replace the values from an array

tag_list = ['tag1', tag4, .. tag_n]

with the id's of a corresponding database model 'tags'

@tags = [{:id => '1', :name => 'tag1'}, {:id => '2', :name => 'tag2' ...}]

The desired output of the 'tag_list' should look like

tag_list_new=['1','4',..'n']

After trying and searching the web, I don't find any solution working for me.

Ps. I am relatively new to rails and really hope for a working answer, thanks in advance!

1 Answer 1

1
  tag_list = ['tag1', tag4, .. tag_n]
  @tags = [{:id => '1', :name => 'tag1'}, {:id => '2', :name => 'tag2' ...}]

  tag_list_new = Array.new 
  tag_list.each do |a|
  @tags.each do |tag|
   if tag["name"].eql?(a) 
    tag_list_new << tag["id"]
   end 
  end 
end
Sign up to request clarification or add additional context in comments.

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.