Skip to content

Instantly share code, notes, and snippets.

@i-arindam
Forked from ericktai/1_user.rb
Created January 4, 2018 05:10
Show Gist options
  • Save i-arindam/237a799e263133a4097a0d73bab82caf to your computer and use it in GitHub Desktop.
Save i-arindam/237a799e263133a4097a0d73bab82caf 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"
},
]
}
]
@i-arindam
Copy link
Author

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