How to check existence by array of uniq numbers in rails - ruby-on-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.

Related

Lua, using table of strings as keys to call values from different tables

What I want
ores = {"Bauxite", "Coal", "Hematite"}
properties={["Bauxite"]={name="Bauxite", density=1.2808},["Coal"]={name="Coal" , density=1.3465},["Quartz"]={name="Quartz" , density=2.6498},["Hematite"]={name="Hematite" , density=5.0398}}
system.print(properties.ores[1].name)
system.print(properties.ores[1].density)
Should Output
Bauxite
1.2808
This is a recurrent question regarding indexing tables by a value rather than by a constant string. You're currently using ((properties.ores)[1]).name; this is not parsed as properties.(ores[1]).name as you seem to have expected. That's because . is always followed by an identifier - a constant string - which is used to index the table. table.key is just shorthand for table["key"] for keys matching [a-zA-Z_][a-zA-Z0-9_]*. If you want to dynamically index a table with some expression, you have to use table[expr].
To fix your code, simply do:
system.print(properties[ores[1]].name)
system.print(properties[ores[1]].density)

Rails 4 Active record, search by attribute that contains an array?

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.

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)
)

Rails where with array returned data order

I just want to make sure that if I used a where query like so:
product1, product2 = Product.where(id: [1, 2]) it will always return the data in that order specified in the array, so the assignment will always be correct.
Is that the behaviour or it might not return the data in that specific order at some time?
Records will always be fetched from database in ascending order by id column (unless you specify another order).
So yea, this is the behavior and you'll always get the assignment right here.

rails combine parameters in controller

Hopefully this is a little clearer. I'm sorry but I'm very new to coding in general. I have multiple tables that I have to query in succession in order to get to the correct array that I need. The following logic for the query is as follows:
this gives me an array based upon the store :id
store = Stores.find(params[:id])
this gives me another array based upon the param .location found in the table store where that value equals the row ID in the table Departments
department = Departments.find(store.location)
I need to preform one last query but in order to do so I need to figure out which day of the meeting is needed. In order to do this I have to create the parameter day_of_meeting found in the table Stores. I try to call it from the array above and create a new variable. In the Table Departments, I there are params such as day_1, day_2 and so on. I need to be able to call something like department.day_1 or department.day_2. Thus, I'm trying to actually create the variable by join the words "department.day_" to the variable store.day_of_meeting which would equal some integer, creating department.day_1...
which_day = ["department.day_", store.day_of_meeting].join("")
This query finds uses the value found from the variable department.day_1 to query table Meeting to find the values in the corresponding row.
meeting = Meeting.find(which_day)
Does this make my problem any clearer to understand?
findmethod can only accept parameters like Meeting.find(1) or Meeting.find("1-xx").
so, what you need is Meeting.find(department.send("day_" + store.day_of_meeting.to_s))
Hope to help!

Resources