chief
chief

Reputation: 740

Return records based on condition

The value for @c is a parameter set by the user. If there is a value I would like to query for Cars with that condition. If @c is nil I would like to return all of the Car records.

     @cars = Car.where('car_type = ?', @c)

Upvotes: 1

Views: 53

Answers (1)

Dylan Markow
Dylan Markow

Reputation: 124429

You can just use a basic check to see if @c is blank:

@cars = @c.blank? ? Car.scoped : Car.where('car_type = ?', @c)

Upvotes: 1

Related Questions