0

I have a model that has a column of type Boolean.

It's possible to retrieve "on the fly" (without iterating all the result) the result of a logic calculus?

For example:

data = Model.limit(10)
# I would like to know if each data.column is true
# the result is something to this logic: data[0].column && data[1].column && ...
# ..but there is one manner to do this without iterating?

Using the main conditions: AND, OR, XOR (the conditions are all the same - all AND or all OR ,...).

damoiser
  • 6,058
  • 3
  • 40
  • 66
  • 1
    Maybe you can treat true as 1 and false as 0. You can easily fetch SUM(column). If SUM(column) == Model.count, it means that all records have column = true. If SUM(column) == 0, it means that all are false. You can easily extend solution to return two additional fields, like number_of_trues [ 1 if true, 0 if false ] and number_of_falses [ 0 if true, 1 if false ]. If you want to know, if all are true, just check if number_of_falses is 0 etc – mkk Aug 05 '13 at 11:03
  • @mkk this is a good solution if you must build from scratch the model too. If the model already exists I think that is better the solution suggested by Ryan Bigg – damoiser Aug 05 '13 at 12:40

1 Answers1

1
records = Model.limit(10).all
records.all? { |r| r.column? } 

I think all? is what you're looking for.

Ryan Bigg
  • 106,965
  • 23
  • 235
  • 261