Ruby on Rails backend API specialist with 8+ years building scalable REST and GraphQL APIs. Deep expertise in ActiveRecord optimization, background jobs, caching strategies, and...
class TrendingPostsService
CACHE_KEY = 'trending_posts:v1'.freeze
CACHE_TTL = 15.minutes
def self.call(limit: 10)
Rails.cache.fetch(CACHE_KEY, expires_in: CACHE_TTL) do
class AddConstraintsToUsers < ActiveRecord::Migration[6.1]
def change
# Null constraints
change_column_null :users, :email, false
change_column_null :users, :username, false
class CreateComments < ActiveRecord::Migration[6.1]
def change
create_table :comments do |t|
t.references :commentable, polymorphic: true, null: false
t.references :author, null: false, foreign_key: { to_table: :users }
t.text :body, null: false
class ProcessPaymentWorker
include Sidekiq::Worker
sidekiq_options queue: :critical, retry: 10
sidekiq_retry_in do |count, exception|
module Webhooks
class StripeController < ApplicationController
skip_before_action :verify_authenticity_token
def create
payload = request.body.read
module Api
module V1
class PostsController < BaseController
def index
# Eager load author and recent comments with their authors
posts = Post.published
class User < ApplicationRecord
has_many :posts, foreign_key: :author_id
validates :email, presence: true,
uniqueness: { case_sensitive: false },
format: { with: URI::MailTo::EMAIL_REGEXP }
production:
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS", 5) %>
timeout: 5000
checkout_timeout: 5
module ApiRequestLogger
extend ActiveSupport::Concern
included do
around_action :log_api_request
end
class AddCommentsCountToPosts < ActiveRecord::Migration[6.1]
def up
add_column :posts, :comments_count, :integer, default: 0, null: false
# Populate existing counts
Post.find_each do |post|
class AddLockVersionToPosts < ActiveRecord::Migration[6.1]
def change
add_column :posts, :lock_version, :integer, default: 0, null: false
end
end
class PostSerializer < ActiveModel::Serializer
attributes :id, :title, :excerpt, :body, :published_at, :views, :likes_count, :comments_count
attribute :can_edit, if: :current_user_can_edit?
belongs_to :author, serializer: UserSummarySerializer