I have the document type celeb. And the document type fact. Each fact references a celeb. I want to list celebs ordered by their most recently updated facts. So the celeb which has just had a fact added about them would show up at the top.
How can I do that?
Answer from Alexander Staubo in the Sanity help channel on Slack
Something like:
*[_type == "celeb"] {
_id,
name,
"latestFact": *[_type == "fact" && celeb._ref == ^._id] | order(_updatedAt desc)[0]
}
| order(latestFact._updatedAt desc)
This assumes that each fact has a field called celeb which is a ref to the celeb document.
For each celeb, it finds the latest fact. At the end, it sorts all the celebs by the date.
Related
I have laravel elqueont collection and i want to sort it based on condition on status value, if status == 3 the ordering will be id desc else the ordering will be id asc, help please.
trying to sort laravel collection based on condition on anthor column value
You can read all about collection sorting on the documentation. So lets say you have 2 conditions your code will look like this
$collection = Model::all();
if($condition == 1) {
$collection->sortBy('created_at');
} else {
$collection->sortByDesc('created_at);
}
This is just a general idea, you can share your code for further help.
I have a database table that looks like this:
My goal is to update my "unavail" table based on the ID of either the component, part, or item depending on which one is relevant in my situation.
For example, if the partID = 43 I want to add to the 'unavail' column
I first started working on this by trying this
db.OffSiteItemDetails.Find(sod.PartID).unavail += sod.comp_returned;
(Where sod.PartId = 43)
But I quickly realized it was just checking for where the "ID" was equal to 43 which isn't what I want. After some investigation I saw people suggesting using
db.Where(x => x.non-pk == value)
So I created this
db.OffSiteItemDetails.Where(x => x.componentID == sod.ComponentID);
But from here I don't know how to change my unavail table values.
This was a tough question to type so if you need more clarity just ask
foreach(var item in db.OffSiteItemDetails.Where(x => x.componentID == sod.ComponentID))
{
// item.unavail = [new value]
// db.Update(item);
// ...I don't know how you update the data in your database
}
Something like that?
I have a following DB schema:
User
- id
- name (String)
UserTag
- user_id
- tag_id
Tag
- id
- key (String)
And i also have a pretty complex users-search chain with a lot of where statements. I'm trying to figure out how to include one another where condition to my chain - filtering for users that has a specific tag assigned (ID of this tag is unknown, just it's key is known).
So, here is more or less how my code looks:
col = User.all
col = col.where('cats_count <= 0') if args[:no_cat]
col = col.where('dogs_count <= 0') if args[:no_dog]
col = col.where('other_pets_count <= 0') if args[:no_other_pet]
# ... tag logic filtering here ...
col = col.where('age > 100') if args[:old]
And i want to filter for a users, who has a Tag with key=non_smoking assigned. Ideally, i would love it to be database-engine independen, if that's important - i'm on sqlite/postgres.
Honestly i have completely no any ideas on how to deal with that, i probably lack some knowledge in SQL matter and then, in Rails/ActiveRecord.
You can specify joins and the put conditions on the joined tables. So maybe something like (untested, off the top of my head)
col.joins(user_tags: :tag).where(user_tags: { tag: { key: 'non_smoking' } })
I have a Work model with a video_id, a user_id and some other simple fields. I need to display the last 12 works on the page, but only take 1 per user. Currently I'm trying to do it like this:
def self.latest_works_one_per_user(video_id=nil)
scope = self.includes(:user, :video)
scope = video_id ? scope.where(video_id: video_id) : scope.where.not(video_id: nil)
scope = scope.order(created_at: :desc)
user_ids = works = []
scope.each do |work|
next if user_ids.include? work.user_id
user_ids << work.user_id
works << work
break if works.size == 12
end
works
end
But I'm damn sure there is a more elegant and faster way of doing it especially when the number of works gets bigger.
Here's a solution that should work for any SQL database with minimal adjustment. Whether one thinks it's elegant or not depends on how much you enjoy SQL.
def self.latest_works_one_per_user(video_id=nil)
scope = includes(:user, :video)
scope = video_id ? scope.where(video_id: video_id) : scope.where.not(video_id: nil)
scope.
joins("join (select user_id, max(created_at) created_at
from works group by created at) most_recent
on works.user_id = most_recent.user_id and
works.created_at = most_recent.created_at").
order(created_at: :desc).limit(12)
end
It only works if the combination of user_id and created_at is unique, however. If that combination isn't unique you'll get more than 12 rows.
It can be done more simply in MySQL. The MySQL solution doesn't work in Postgres, and I don't know a better solution in Postgres, although I'm sure there is one.
ActiveRecord objects of the class 'Location' (representing the db-table Locations) have the attributes 'url', 'lat' (latitude) and 'lng' (longitude).
Lat-lng-combinations on this model should be unique. The problem is, that there are a lot of Location-objects in the database having duplicate lat-lng-combinations.
I need help in doing the following
Find objects that share the same
lat-lng-combination.
If the 'url' attribute of the object
isn't empty, keep this object and delete the
other duplicates. Otherwise just choose the
oldest object (by checking the attribute
'created_at') and delete the other duplicates.
As this is a one-time-operation, solutions in SQL (MySQL 5.1 compatible) are welcome too.
If it's a one time thing then I'd just do it in Ruby and not worry too much about efficiency. I haven't tested this thoroughly, check the sorting and such to make sure it'll do exactly what you want before running this on your db :)
keep = []
locations = Location.find(:all)
locations.each do |loc|
# get all Locations's with the same coords as this one
same_coords = locations.select { |l| l.lat == loc.lat and \
l.lng == loc.lng }
with_urls = same_coords.select { |l| !l.url.empty? }
# decide which list to use depending if there were any urls
same_coords = with_urls.any? ? with_urls : same_coords
# pick the best one
keep << same_coords.sort { |a,b| b.created_at <=> a.created_at }.first.id
end
# only keep unique ids
keep.uniq!
# now we just delete all the rows we didn't decide to keep
locations.each do |loc|
loc.destroy unless keep.include?( loc.id )
end
Now like I said, this is definitely poor, poor code. But sometimes just hacking out the thing that works is worth the time saved in thinking up something 'better', especially if it's just a one-off.
If you have 2 MySQL columns, you can use the CONCAT function.
SELECT * FROM table1 GROUP BY CONCAT(column_lat, column_lng)
If you need to know the total
SELECT COUNT(*) AS total FROM table1 GROUP BY CONCAT(column_lat, column_lng)
Or, you can combine both
SELECT COUNT(*) AS total, table1.* FROM table1
GROUP BY CONCAT(column_lat, column_lng)
But if you can explain more on your question, perhaps we can have more relevant answers.