So I perform a query to the db and I have a complete array of objects:
@attachments = Job.find(1).attachments
Now that I have an array of objects I don't want to perform another db query, but I would like to filter the array based on the Attachment
object's file_type
so that I can have a list of attachments
where the file type is 'logo'
and then another list of attachments
where the file type is 'image'
Something like this:
@logos = @attachments.where("file_type = ?", 'logo') @images = @attachments.where("file_type = ?", 'image')
But in memory instead of a db query.
One can use filter() function in JavaScript to filter the object array based on attributes. The filter() function will return a new array containing all the array elements that pass the given condition. If no elements pass the condition it returns an empty array.
You can use the select method in Ruby to filter an array of objects. For example, you can find all the even numbers in a list. That's quite a bit of code for something so simple!
Array#select() : select() is a Array class method which returns a new array containing all elements of array for which the given block returns a true value. Return: A new array containing all elements of array for which the given block returns a true value.
The filter() is an inbuilt method in Ruby that returns an array which contains the member value from struct which returns a true value for the given block. Parameters: The function accepts a single parameter block which specifies the condition.
Try :
This is fine :
@logos = @attachments.select { |attachment| attachment.file_type == 'logo' } @images = @attachments.select { |attachment| attachment.file_type == 'image' }
but for performance wise you don't need to iterate @attachments twice :
@logos , @images = [], [] @attachments.each do |attachment| @logos << attachment if attachment.file_type == 'logo' @images << attachment if attachment.file_type == 'image' end
If your attachments are
@attachments = Job.find(1).attachments
This will be array of attachment objects
Use select method to filter based on file_type.
@logos = @attachments.select { |attachment| attachment.file_type == 'logo' } @images = @attachments.select { |attachment| attachment.file_type == 'image' }
This will not trigger any db query.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With