Skip to content

Instantly share code, notes, and snippets.

@ericktai
Created July 28, 2017 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save ericktai/19e4e88bc98e48bb282166298c641f10 to your computer and use it in GitHub Desktop.
Save ericktai/19e4e88bc98e48bb282166298c641f10 to your computer and use it in GitHub Desktop.
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
Copy link
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