2

Should be an easy one and might have over complicated the title somewhat.

I have a variable that contains records:

@record = Records.all

and an array that holds some task_id's:

@array #has for e.g. [1,2,3]

What i want to do check the task_id column of the list of records in @records to see if they contain any of the numbers in the array. If they do then i want those numbers to be put into another array.

i know this is simple but i keep getting confused along the way as im quite new to ruby syntax.

1 Answer 1

6

This should work for you:

@records.map(&:task_id) & @array

This builds the intersection of the two lists (task_ids and array). You can try this example at the console (I hope this helps to clear up how it works):

irb(main):008:0> a =  [1,2,3,4]
=> [1, 2, 3, 4]
irb(main):009:0> b = [3,4,5,6]
=> [3, 4, 5, 6]
irb(main):010:0> a & b
=> [3, 4]
Sign up to request clarification or add additional context in comments.

1 Comment

im getting a wrong number of arguments(1 for 0) error with this one.

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.