Search code examples
ruby-on-railsrubypostgresqlactiverecordaasm

Efficiently fetching records on multiple condition


Using aasm statemachine for model Booking.

The state machine have below states

 state :payment_authorized
 state :payment_captured
 state :payment_failed
 state :some_more_states

So to fetch bookings based on state I can call booking.payment_authorized. But what am looking for is I want all records with state payment_authorized and payment_captured.

This works booking.payment_authorized + booking.payment_captured but this is doing 2 database calls. one for finding authorized_state and other for captured_state. How can I do this more efficiently(in one call)?

Thank you


Solution

  • You can use Booking.payment_authorized.or(Booking.payment_captured) to get all records in a single query.