Rails and select
In a rails project I’m working on, I was trying to select from the database some sales with some conditions.
Here is the Sale
model:
class Sale < ActiveRecord::Base
belongs_to :voucher
belongs_to :client
has_many :line_items, dependent: :destroy
has_many :bills
end
So the goal of the task was to obtain all the Sales
where the LineItems
has express_checkout
set it to true.
I thought it was easy. This is my initial code:
Sales.all.select do |sale|
sale.line_items.select do |line_item|
line_item.express_checkout == true
end
end
So I saw this code and thought that must be right, but I kept getting all the Sales
.
A friend told me to extract a method for this kind of check into the Sales' model, so I tried it and created a new method called
express_checkout?`.
def express_checkout?
l = line_items.select{|line_item| line_item.express_checkout == true}
l.any?
end
This method did the same, but instead, I stored the result of the select result in a variable and then checked if there were any objects inside that variable. That approach worked.
So I thought select
always returns an array with the elements that passed from the condition, but I didn’t know we have to store them in a variable and then check it.
The final code:
Sales.all.select do |sale|
sale.express_checkout?
end