Fellow Stranger
Fellow Stranger

Reputation: 34093

Get all objects that has same enum value as an instance object's enum value

class Car < ActiveRecord::Base
  enum colors: {blue: 0, red: 1}
end

my_car.color # => "blue"

How do I get all cars with the same enum value as a certain object? I.e. in the case above, all blue cars.

This is my best bet so far:

Car.public_send(my_car.color)

Upvotes: 0

Views: 54

Answers (1)

zwippie
zwippie

Reputation: 15515

Get all cars with the same color as my_car:

Car.where(color: my_car.color)

Or if that doesn't work:

Car.where(color: Car.colors[my_car.color])

Upvotes: 1

Related Questions