I am working on a rails 4.2.5.1 application where I am using a custom scope :
scope :listing_search_cities, ->{group(:city).count}
I was using this scope to create facets and filters. Everything was working fine on MYSQL. When I pushed this to Heroku, due to PG I got the error that created_at must be added to the group function.
I did that, the error went away and my scope is now:
scope :listing_search_cities, -> {group('city, created_at').count}
However, this gives a hash of created_at time and count like:
{2018-10-10....UTC => 2}
and not city name and count which is what I want(and the query gives this in MYSQL)like
{ "tempe" => 2}
I just want the city name and count of that selection(search results) I provide to the scope.
How can I do it in PG where on a search result on listings, I can get the city names and count of each city in a hash using group and count?
I read elsewhere about people using unscoped? How to use that?
Nevermind! For those who face the same problem: Just reverse the order of group:
scope :listing_search_cities, -> {group('created_at, city').count}
and you are good to go.
Related
I have just created an activity with the name of InventorParams. I would like to add an alias as right now it shows up as: Guasamt.InventorParams+$LATEST in the activities list.
Now when POSTing the following request:
[
"id" => "prod"
]
Laravel will turn this into json. The Authorization header is also present
to https://developer.api.autodesk.com/da/us-east/v3/activities/:id/aliases. It either returns 400 or 404 errors. The 400 error is presented when using a fully qualified activity name. The 404 error is returned on all of the the following activity names:
Guasamt.InventorParams+LATEST (without $);
Guasamt.InventorParams;
InventorParams;
Now I'm wondering what should be the unqualified name of an activity?
When you create an Activity in FDA it gets a version number (since you just created it, it would be 1). This number needs to be used when creating Activity Alias, which is later used in the Work Item.
Your fully qualified name consist of <your_nick_name>.<activity_name>+<activity_version>. Where <your_nick_name> can be your ClientId if nickname was not specified.
Anyway I would recommend to follow the FDA tutorial. Another option on how to get quickly up to speed with FDA is to use the Autodesk Forge Tools addin to VS Code.
This quite frankly is driving me up the wall. (connecting to a node.js server that needs the order id)
The post id is whats actually used when using the /wp-json/wc/v3/orders/
So something like = /wp-json/wc/v3/orders/259992?customer_key ... Works but using the "number" item which is connected to this (the order number) it errors. For example's sake we will say the order number linked with the order post id I used above (259992) = 99794
I've been trying to get the result by order number.
If I run /wp-json/wc/v3/orders?customer_key ... it lists all the orders no problems. However if I attempt to run /wp-json/wc/v3/orders?select?q=number:99794?consumer_key= ... I get the following error (i have also tried this with ?search?q=number:99794?) no no avail
Which is daft because i know you can list resources. I just did it with /wp-json/wc/v3/orders?customer_key ...
I just need it to return the json for the order id not the post id. Is this at all possible?
Have also tried: /wp-json/wc/v3/orders?filter[number]=99798?customer_key ... Same error
I have models:
class Reference(models.Model):
name = models.CharField(max_length=50)
class Search(models.Model):
reference = models.ForeignKey(Reference)
update_time = models.DateTimeField(auto_now_add=True)
I have an instance of Reference and i need to get all last searches for the reference. Now i am doing it in this way:
record = Search.objects.filter(reference=reference)\
.aggregate(max_date=Max('update_time'))
if record:
update_time = record['max_date']
searches = reference.search_set.filter(update_time=self.update_time)
It is not a big deal to use 2 queries except the one but what if i need to get last searches for each reference on a page? I would have got 2x(count of references) queries and it would not be good.
I was trying to use this solution https://stackoverflow.com/a/9838438/293962 but it didn't work with filter by reference
You probably want to use the latest method.
From the docs, "Returns the latest object in the table, by date, using the field_name provided as the date field."
https://docs.djangoproject.com/en/1.8/ref/models/querysets/#latest
so your query would be
Search.objects.filter(reference=reference).latest('update_time')
I implemented a snippet from someone in gist but I don't remember the user neither have the link.
A bit of context:
I have a model named Medicion that contains the register of mensuration of a machine, machines are created in a model instance of Equipo, Medicion instances have besides of a Foreign key to Equipo, a foreign key to Odometro, this model serves as a kind of clock or metre, that's why when I want to retrieve data (measurements aka instances of Medicion model) for a certain machine, I need to indicate the clock as well, otherwise it would retrieve me a lot of messy and unreadable data.
Here is my implementation:
First I retrieve the last dates:
ult_fechas_reg = Medicion.objects.values('odometro').annotate(max_fecha=Max('fecha')).order_by()
Then I instance an Q object:
mega_statement = Q() # This works as 'AND' Sql Statement
Then looping in every date retrieved in the queryset(annotation) and establishing the Q statement:
for r in ult_fechas_reg:
mega_statement |= (Q(odometro__exact=r['odometro']) & Q(fecha=r['max_fecha']))
Finally passed this mega statement to the queryset that pursues to retrieve the last record of a model filtered by two fields:
resultados = Medicion.objects.filter(mega_query).filter(
equipo=equipo,
odometro__in=lista_odometros).order_by('odometro', 'fecha') # lista_odometros is a python list containing pks of another model, don't worry about it.
I have the following query
#initial_matches = Listing.find_by_sql(["SELECT * FROM listings WHERE industry = ?", current_user.industry])
Is there a way I can run another SQL query on the selection from the above query using a each do? I want to run geokit calculations to eliminate certain listings that are outside of a specified distance...
Your question is slightly confusing. Do you want to use each..do (ruby) to do the filtering. Or do you want to use a sql query. Here is how you can let the ruby process do the filtering
refined list = #initial_matches.map { |listing|
listing.out_of_bounds? ? nil : listing
}.comact
If you wanted to use sql you could simply add additional sql (maybe a sub-select) it into your Listing.find_by_sql call.
If you want to do as you say in your comment.
WHERE location1.distance_from(location2, :units=>:miles)
You are mixing ruby (location1.distance_from(location2, :units=>:miles)) and sql (WHERE X > 50). This is difficult, but not impossible.
However, if you have to do the distance calculation in ruby already, why not do the filtering there as well. So in the spirit of my first example.
listing2 = some_location_to_filter_by
#refined_list = #initial_matches.map { |listing|
listing.distance_from(listing2) > 50 ? nil : listing
}.compact
This will iterate over all listings, keeping only those that are further than 50 from some predetermined listing.
EDIT: If this logic is done in the controller you need to assign to #refined_list instead of refined_list since only controller instance variables (as opposed to local ones) are accessible to the view.
In short, no. This is because after the initial query, you are not left with a relational table or view, you are left with an array of activerecord objects. So any processing to be done after the initial query has to be in the format of ruby and activerecord, not sql.
I am writing a query in a mysql db website and need a little help on the select statement syntax. I need to retrieve info from a database containing user input from a web-form at my site. There are a large number of over-the-road drivers who need to check in reporting their location with this form. The goal is to have a concise grid display table on the resulting web page that shows only the latest entry for all the drivers with only the 3 criteria on each row, (– name – location – date/time). The code included below does return results for all the drivers, but in the form of a long list of all entries for each driver on multiple pages instead of just updating location for each driver. It seems like I need to use “DISTINCT” and/or “LIMIT” in the string, but my attempts at this haven’t produced the desired results.
Thanks, Mike
$myquery = "select name, location, recordtime from ".$config->site_db."form_db order by recordtime DESC";
If I understand correctly you want to select the record with the highest recordtime for each name:
$myquery = "select name, location, max(recordtime) from ".$config->site_db."form_db group by name";