Rails 3 model associations. Limit the number of data selected - ruby-on-rails

I have a model user, a feed, and a comment. user has many feeds and comments, feeds belong to user and have many comments and finally comments belong to user and feed.
No when i show feeds for a certain user, it selects all the comments made till date for every feed. I want it to select only those comments that were made in the last 1 min, and if none were, i want to show the 2 most recent ones... how can i do that???

Related

Need a SUMIF =SUM(C3-(SUM(E3:N3))) but if E14<0 dont subtract

I have simple problem which I can't get the right twist on it.
I have a list of "products" with their stock in the range of D3:D13 with a set price, an original stock and a list of "customers" in the range of E3:L3.
Each customer can take a product and the updated stock currently is:
[=SUM(C3-(SUM(E3:L3))) ]
If we take one person he has his combined cost for how many products he took on E14
If a person pays their cost (E14 > 0) and nulls the products he took currently the updated stock gets rested. That is what I dont want.
Basically: SUM Stock IF (any of E14:L14) > 0 THEN =SUM(C3-(SUM(E3:N3)))
Update:
Example:
https://docs.google.com/spreadsheets/d/1SEssivZ8jCbqedc8TG_PO4uWLN_QzpRQwxJ_bGwHvQg/edit?usp=sharing Hope the link works.
That is basically everything and this works for me currently.
The issue is if a user decides to pay their dept.
For example User 1 pays.
Then the currently calculated price for the user (E14) get's nulled and also the cells for how much products they took (E3:E12).
But then because how it is done currently the updated stock gets updated again with the items the user1 took previously because we nulled the E3:E12. But the products where already taken. That is what I want to prevent.

Firestore query with array of strings

I try to make social network (like instagram )
User have posts and followers etc
I want for home feed show posts just from users i follow.
User have array with ids of users he follow. So idea is to query from colloection “Posts” where id is equal to one of ids and sort them by timestamp and limit them.
I could make for loop for each in following array but this will be unusable for more than 100 following users.
Edit how I think it could be solved
Hello i dont know if im doing it right but here is my solution for social network like instagram or any other where one user follow other users and have feed from their posts ordered by date.
Each user have its own Collection Timeline where are posts references (postId, createdAt, createdBy) that user should see at his feed, when user start following other user it will put all his posts to feed. For display feed I just call on User timeline, sort it by date and request just 25 of them.
When user start the app I loop trough all followers and request their posts younger than lastTimeline timestamp from their posts collection if they have any I add them to timeline.
I would be happy for your opinion.
You can loop through all the followed user ID and then request the first 5 posts of each one.
Or use some kind of paging

Query limited data in firebase

I am working on an application that retrieves the posts from firebase. The problem is I want to retrieve the first 20 posts and show in a table. When a user clicks the next button, it will retrieve the next 20 posts and show and so on.
I know firebase provides the following methods
queryLimitedToFirst
queryLimitedToLast
queryStartingAtValue
queryEndingAtValue
queryEqualToValue
But how do I use them in conjunction to retrieve the desired results like I want?
Here is the structure of the data I have:
You should order query by some property (I usually use timestamp). This organises your orders from latest to oldest. When you have them ordered you should limit them:
queryOrdered(byChild: "timestamp").queryLimited(toLast: UInt(limit))
As you can see we can limit results to whatever number we like. You can use this to read your posts each time for 20 results more.
So the first time you call it with a limit of 20, next time with a limit of 40, etc. Keep in mind that this will return posts that were already returned in the previous call.
Instead you could combine it with queryStartingAtValue and use your last post tiemstamp - this will "skip" your previous results and return only 20 posts you need.
This is just an idea but I think it should work like expected.

Joining Meteor collections for scoring

Let's say I have two collections: "Movies" and "Reviews." Users write reviews about movies and assign each movie a review score from 0 to 10. I want to make a list of the top 20 movies, based on each movie's average review score. Does anyone know how to set up the publish/subscription code? It's giving me the blues now. Everything I've read suggests this is a weak area for Meteor, and I can't seem to find a comparable example.
The solution is to store the average review score directly in the Movies collection and update it when a new review is added. This can be achieved by storing the total review score and number of reviews, which lets you update the average without having to retrieve all of the other reviews.
Update the average for a movie each time a review is posted and store that average in the movie document. It is more work at the time of creating the review, but querying the top 20 movies becomes trivial after that.

Rails - Finding the count of user's who have created posts

I wish I could give you guys some code but this question is more of a math problem and doesn't need code to solve.
I'm trying to find a number. The number shows the total count of users who have created Posts. Say my app has 25 posts and those posts come from 4 different users, then the count equals 4. How can I get this count??
The count is for this
#collections = Collection.all
Finding the number of users who have created a Collection.
Thanks so much.
You can do this:
Collection.distinct.count(:user_id)
This will execute a count query and only return the number of unique users who created a Collection.

Resources