Rails 4 Active record, search by attribute that contains an array? - ruby-on-rails

I want to search by an attribute that contains an array. I'm interested in returning all records where the array in this attribute contains a specific value.
example object
Location_1 {
regions: ["on", "qc"]
}
I want to do something like this Location.where(regions: "on"), but I'm not sure of the correct syntax.
what is the right way to do this?

Try this Location.where('regions in (?)', ['on','qc'])
The IN operator allows you to specify multiple values in your WHERE clause.

Related

How to check existence by array of uniq numbers in rails

I have Disclosure model that have accession_number column. The column have unique constraint.
And when there is an array of accession_numbers, how can I know if there is accession_numbers that is not used yet.
I'm currently check existence for every numbers, but I think there is better way for this behavior.
accession_numbers.select{|number| !Disclosure.where(accession_number: number).exists?}
You can query for all disclosures which have an accession_number in your array.
existing = Disclosure.where(accession_number: accession_numbers).pluck(:accession_number)
Then just remove the existing ones from your array
accession_numbers - existing
Since you already have an unique constraint at DB level,
exiting_accessions = Disclosure.pluck(:accession_number)
results in an array of existing accesssions.
accession_array - existing_accessions results in an array of unused accessions.

How do I extract a field from an array of my models in order to form a single query?

I’m using Rails 4.2.7. I have an array of my model objects and currently I’m iterating through that array to find matching entries in the database based on a field my each object …
my_object_times.each_with_index do |my_object_time, index|
found_my_object_time = MyObjectTime.find_by_my_object_id_and_overall_rank(my_object_id, my_object_time.overall_rank)
My question is, how can I rewrite the above to run one query instead of N queries, if N is the size of the array. What I wanted was to force my underlying database (PostGres 9.5) to do a “IF VALUE IN (…)” type of query but I’m not sure how to extract all the attributes from my array and then pass them in appropriately to a query.
I would do something like this:
found_my_object_times = MyObjectTime.where(
object_id: my_object_id,
overall_rank: my_object_times.map(&:overall_rank)
)

ActiveRecord, get only one field (not id) and flatten result array

I have a table and want to get one specific field from every row. When I do select I always get id, which I don't want. After that I want to have the result in a simple array.
This is what I do:
User.all.select(:reg)
This is what I get:
[{"id":null,"reg":"erfa"},{"id":null,"reg":"jhzhegrwe"}]
This is what I want:
{"erfa","jhzhegrwe"}
pluck i believe is what you want, which converts database results in a Ruby array.
This will only return column :reg
User.all.pluck(:reg)

Mongoid order by length of array

How to sort the Mongoid model by the length of the array which is a field inside the model.
Mongo documentation says:
You cannot use $size to find a range of sizes (for example: arrays
with more than 1 element). If you need to query for a range, create an
extra size field that you increment when you add elements. Indexes
cannot be used for the $size portion of a query, although if other
query expressions are included indexes may be used to search for
matches on that portion of the query expression.
So we cannot order by using mongo's $size.
You can solve your task by adding new field, which will store array size.
class Post
include Mongoid::Document
field :likes, type: Array, default: []
field :likes_size, type: Integer
before_save do
self.likes_size = likes.size
end
end
Sort posts by likes_size:
Post.order_by(likes_size: :desc)
Document says that you can't orderby using size.
Try adding a new column containing the value of size and sort it which will work as order by.
In ruby, you can sort an array like this :
my_array.sort_by(&:my_attr)
It will sort the array my_array by the attribute my_attr of each element inside the array.
You can also write it like this :
my_array.sort_by{|element| element.my_attr }
Which is exactly the same, it will sort by the my_attr attribute of each element. This second syntax is for when you want a more complex sort condition than just the result of a method of each element.
Documentation : http://ruby-doc.org/core-2.3.1/Enumerable.html#method-i-sort_by

What is simplest way to convert :pluck or :collect results to array of strings in Rails?

I have a model called Record and belongs Product model,
the price column type is hexadecimal. Rails already convert it to string in view. But I wanna get them in my console queries.
Example code for pluck:
Product.first.records.pluck(:price)
This query displays the values in array as hexadecimal. There is a method called to_sentences for pluck values but its not enough for my case.
The problem is same in collect method:
Product.first.records.collect(&:price)
What is the pluck query to display my hexadecimal data as array of strings like:
["45,46","75,42"]
Product.first.records.pluck(:price).map(&:to_s)
please try this:
Product.first.records.pluck(:price).join(',')

Resources