How to search for ancestors in a nested set model? - mysql

I am using Ruby on Rails v3.2.2, MySQL database and the AwesomeNestedSet gem in order to handle a nested set model data structure for places (addresses, cities, countries and so on). Columns present in the related places database table are name, parent_id, lft and rgt.
I would like to implement a method so to find records by their names and by related ancestor names. That is, for instance, when I search for "Spacca Napoli Street, Naples" (note: the comma , in the string is used as a constant separator) then I would like to return addresses with name Spacca Napoli Street having an ancestor (in my case, a city) with name Naples.
Is it possible (maybe, by using some RoR scope method)? If so, how to make that?

Related

Rails, MySql, JSON column which stores array of UUIDs - Need to do exact match

I have a model called lists, which has a column called item_ids. item_ids is a JSON column (MySQL) and the column contains array of UUIDs, each referring to one item.
Now when someone creates a new list, I need to search whether there is an existing list with same set of UUIDs, and I want to do this search using query itself for faster response. Also use ActiveRecord querying as much as possible.
How do i achieve this?
item_ids = ["11E85378-CFE8-39F8-89DC-7086913CFD4B", "11E85354-304C-0664-9E81-0A281BE2CA42"]
v = List.new(item_ids: item_ids)
v.save!
Now, how do I check whether a list exists which has item ids exactly matches with that mentioned in query ? Following wont work.
list_count = List.where(item_ids: item_ids).count
Edit 1
List.where("JSON_CONTAINS(item_ids, ?) ", item_ids.to_json).count
This statement works, but it counts even if only one of the item matches. Looking for exact number of items.
Edit 2
List.where("JSON_CONTAINS( item_ids, ?) and JSON_LENGTH(item_ids) = ?", item_ids.to_json, item_ids.size).count
Looks like this is working
You can implement a has many relation between lists and items and then access like this.
List.includes(:item).where('items.id in (?)',item_ids)
To implement has_many relation:
http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association

How to find last item in a repeated structure in bigquery

I have a nested repeated structure, the repeated structure is of variable length. For example, it could be a person object with a repeated structure that holds cities the person has lived in. I'd like to find the last item in that list say to find current city person lives in. Is there an easy way to do this, I tried looking around jsonpath functions but I'm not sure how to use it with "within". Any help please?
1) You can use LAST and WITHIN
SELECT
FIRST(cell.value) within record ,
LAST(cell.value) within record
FROM [publicdata:samples.trigrams]
where ngram = "! ! That"
2) or if you want something more advanced you can use POSITION
POSITION(field) - Returns the one-based, sequential position of field within a set of repeated fields.
You can check the samples from trigrams (click on Details to see the unflatten schema)
https://bigquery.cloud.google.com/table/publicdata:samples.trigrams?pli=1
And when you run POSITION, you get the ordering of that field.
SELECT
ngram,
cell.value,
position(cell.volume_count) as pos,
FROM [publicdata:samples.trigrams]
where ngram = "! ! That"
Now that you have the position, you can query for last one.

Create a double join query using Rails Admin

I am trying to create a form using Rails Admin, version 4.0.0. For this form I have tables called items, locations, cities and item_in_city. Tables are related in the following ways:
items table has a column called location which is array or location ids where item is present
location table has a column called city_id which connects it to city table
there is item_in_city table which has item_id and its corresponding list of city_id
Now, I want to create a form using Rails Admin, where I can give option to add a new item. While adding this new item I want to give an option to select (multiple) cities and corresponding to cities I want to give list of locations which can be selected.
I figured out the solution, So I thought of posting it as it can be useful to others. Combination of city name and location can be posted by defining an enum method in the following way
def location_enum
Location.all.collect {|l| [ l.city.name + ' ' + l.name, l.id] }.sort
end

Build and Array of Arrays from query result with <cfloop>

I've got records in my MySQL projects database that have several boolean flags to help me sort the data. I have 3 categories planning, landscape, and environmental and 4 classes (or subcategories) for each category; industrial, government, education, residential.
My goal is to use ColdFusion to create and store the project_id numbers in an array of some kind that will basically have the projects sorted by category and class. That way I can grab just the industrial projects in the planning category and construct a link to that project.
So, the first position in the array would be planning and inside that first position would be the 4 classes, then, within that would be all of the project_id numbers that returned true for those particular criteria.
The logic I'm looking to create goes like this...
Loop over the query result, if planning = true and industrial = true, place the project id # into the planning array inside the industrial array.
How can I use <cfloop> to loop over the list of project records, recognize the category and class flags and construct a clean and usable dataset? Can this be handles in the query in some way?
Figure out the desired data structure
look at your existing data structure
figure out the algorithm to translate from one to the other
You may cfloop the query, and use a large cfswitch (or large set of if-then-else) to figure out how you want to store the id in your desired data structure. Or if you can map the class/category name as a struct key, then it might be easier.
Or you may use cfoutput group="" if it helps (cfloop group="" is available on in CF10+)
Finally, maybe you don't even need the data structure, just use Query of Queries wherever you need.
You may be able to utilize the Underscore.cfc library for this. For example, you could use the filter function to extract an array of structs representing the query rows you want:
planningArray = _.filter(queryResult, function(row){
return (row.planning == true && row.industrial == true);
});

Assigining ID vs object - linq to sql

Say I have an entity Customer which has relationship with city, order etc. Now when I am adding a customer object, should I assign customer.cityid, or customer.city? From the form I get cityid from a dropdown, so to assign the city object, I will have to make a query using id selected.
If you need the City object populated, then get the City and set .City.
If you don't need the City object and are just saving and moving on, setting .CityId without getting the city object will save you the select query fetching it.
In either case, the next time the object loads, City will be available. (Both methods save the same City column in the database unless you have something strange/non-default setup).