4

I have record of students like

{"id"=>"14", "first_name"=>"Donald", "last_name"=>"Trophy", "age"=>"13", "gender"=>"male", "cast"=>"black", "fee_status"=>"paid", "deleted_at"=>nil}

To send data to DataTable I am taking some columns

patient.slice('age', 'gender', 'cast', 'fee_status').values

I have another array coming from some flow, hidden_columns which can have following value:

["age"]

["age", "gender"]

["31", "33", "age"]

["31", "gender", "33", "age"]

I want to except the values I have in hidden_columns

What I am trying is:

patient.slice('age', 'gender', 'cast', 'fee_status').except(hidden_columns).values

which is not working for me.

1
  • Why not go ahead and do it? Commented Oct 26, 2018 at 11:47

2 Answers 2

5

You'll have to use the splat operator inside except as it accepts multiple keys as arguments, not an array of keys,

patient.slice('age', 'gender', 'cast', 'fee_status').except(*hidden_columns)
 => {"cast"=>"black", "fee_status"=>"paid"}
Sign up to request clarification or add additional context in comments.

Comments

1

First of all you have to use splat (*) operator. Then instead of using .slice() and .except() together, you can do this is more efficient way.

columns_to_show = ['age', 'gender', 'cast', 'fee_status']
columns_to_show = columns_to_show - hidden_columns if hidden_columns

patient.slice(*columns_to_show).values

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.