1

My Detail model have fields like:

email1,email2,email3  

How do I get all the values, of a particular record, of these fields at once.
Something like:

detail = Detail.find(id)
output = detail.only(:email1,:email2,email3)  

=> ["email1","email2","email3"]    #desired output.  

How can I do this?

1 Answer 1

3

You can access the attributes as a hash and call values_at with multiple arguments:

detail.attributes.values_at('email1', 'email2', 'email3')
#=> [...]

One caveat is that the keys are stored as strings, so you'll need to convert your symbols first.

Sign up to request clarification or add additional context in comments.

4 Comments

what if few of them are nil?
email1,email2,email3 and so on
The keys or the values?
the value. okay. find out myself. it's returning empty string.

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.