0

My user model has seven boolean columns (:mon, :tue... :sat, :sun) for each day of the week. In one of the user methods I need to create an array of strings like ["mon", "tue"... "sat", "sun"] for use in time parsing. As you can guess, these array values come from the database columns, and should only be in the array if that column is true.

One way to do it would be manually, like so:

if user.mon
  arr.push("mon")
end

... for every day in the user model. However, I was wondering if there was a cleaner, more efficient way of accomplishing this.

1 Answer 1

1

I think you want select:

 ["mon", "tue"... "sat", "sun"].select {|day| user.send day }
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.