This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Admins | |
class SubscriptionsContoller < ApplicationController | |
# This is left empty because the instruction clearly says the subscriptions.html.erb | |
def index; end | |
# eager loading the subscription entity to include the plan associated data | |
def subscriptions | |
@subcriptions = Subscription.order(created_at: :desc).includes(:plan).page(params[:page]).per(100) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/****** app/assets/stylesheets/admins/subscriptions.css ****/ | |
body { | |
@media (min-width: 768px) { | |
.subscription { | |
display: grid; | |
} | |
} | |
@media (min-width: 1024px) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The problem right now is due to N+1 queries, which means, our database is being flooded with 1, 000, 000 + 1 queries (Since we have a million subscriptions currently)—this is because Ruby on Rails lazy loading enabled by default. So to improve this, we eager load all the subscription entity with ‘includes()’. This allows to preload the plan associated data and thereby saving us some time. There are also other ways to eager load subscription entity with ‘eager_load()’ and ‘preload()’. | |
# lib/scripts/output_subscribers.rb | |
puts "email,monthly_price" | |
Subscription.includes(:plan).where.not(plans: {id: nil}).find_each do |subscription| | |
puts "#{subscription.email},#{subscription.plan.monthly_price}" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const testArr = [ | |
{ | |
name: 'Ade', | |
ranking: 1 | |
}, | |
{ | |
name: 'Luqman', | |
ranking: 2 | |
} | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const testArr = [ | |
{ | |
name: 'Ade', | |
ranking: 1 | |
}, | |
{ | |
name: 'Luqman', | |
ranking: 2 | |
} | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import chai from 'chai'; | |
import chaiHttp from 'chai-http'; | |
import app from '../../../server'; | |
describe('Get no article', () => { | |
it('should not get an article that does not exist', async () => { | |
const res = await chai.req(app) | |
.get('/api/v1/articles/234'); | |
expect(res.status).to.be.equal(404); | |
expect(res.body.message).to.be.deep.equals('Article not found'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import chai from 'chai'; | |
import chaiHttp from 'chai-http'; | |
import app from '../../../server'; | |
import data from '../model/ariticles'; | |
describe('Get no article', () => { | |
it('should not get an article that does not exit', async () => { | |
const res = await chai.request(app) | |
.get('/api/v1/articles/234'); | |
expect(res.body.status).to.be.equal(404); |