Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class User < ApplicationRecord
has_many :posts
has_many :comments
# id :integer not null, primary key
# name :string(50) default("")
end
class Post < ApplicationRecord
belongs_to :user
has_many :comments
# id :integer not null, primary key
# user_id :integer
# content :text default("")
# created_at :datetime
end
class Comment < ApplicationRecord
belongs_to :user
belongs_to :post
# id :integer not null, primary key
# user_id :integer
# post_id :integer
# content :text default("")
# created_at :datetime
end
class NewsfeedController < ApplicationController
# JSON endpoint that returns an array of Post objects in order of
# newest first, to oldest last. Each Post contains a User object
# (the author of the Post), and an array of Comments. Each Comment
# will also include the User object of the Comment's author.
# TODO: Newsfeed endpoint here
end
[
{
"type": "Post",
"content": "First post",
"user": {
"type": "User",
"name": "Luke"
},
"comments": [
{
"type": "Comment",
"user": {
"type": "User",
"name": "Leia"
},
"content": "First comment"
},
{
"type": "Comment",
"user": {
"type": "User",
"name": "Han"
},
"content": "Second comment"
},
]
},
{
"type": "Post",
"content": "Second post",
"user": {
"type": "User",
"name": "Darth Vader"
},
"comments": [
{
"type": "Comment",
"user": {
"type": "User",
"name": "Boba Fett"
},
"content": "Third comment"
},
{
"type": "Comment",
"user": {
"type": "User",
"name": "Jabba"
},
"content": "Fourth comment"
},
]
}
]
@ericktai

This comment has been minimized.

Copy link
Owner Author

ericktai commented Jul 28, 2017

Reflektive has a newsfeed page - liken it to Facebook posts or LinkedIn Posts - where a User can create a message Post. Others can Comment on the post. The skeleton models are provided above.

The application will fetch posts from the newsfeed_controller. Presume that there are hundreds of posts per day. There may be a web app or mobile app or 3rd party consumer.

Build out the newsfeed_controller so that someone can fetch newsfeeds and produce the JSON above. You can presume that you only fetch 50 at a time.

Feel free to copy this gist and add your code to a gist. You do not have to have this be a running Rails app, but please use real code in your example (not pseudo code)

some thoughts: Can you make the queries efficient (what would this mean)? What if we want to reuse the query in other tasks? Would you organize the code in a different way? (e.g. Email the most recent 50 posts to somebody).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.