coding-in-ruby-on-rails
Installation
SKILL.md
Follow these rules strictly when coding in Ruby on Rails to ensure a maintainable, high-performance, and secure codebase.
1. General Rails Structure
- Follow Rails Conventions: Adhere strictly to the standard MVC architecture and Rails directory structure. Follow rails convention while writing code.
- The Rails Way: Use built-in Rails features (helpers, callbacks, etc.) before implementing custom solutions.
2. Fat Models & Skinny Controllers
- Skinny Controllers: Controllers should only handle requests (params, authentication, session) and responses (rendering, redirection). Business logic must not reside in controllers.
- Fat Models: Business logic, complex validations, and data transformations belong in models. If a method can be used multiple times or contains complex logic, it must reside in its suitable model.
- Scopes: Use model scopes for common database queries to keep logic reusable and readable.
- Query Objects: For complex queries involving multiple tables, use Query Objects.
3. Service Objects
- Business Workflows: Use Service Objects (e.g.,
app/services) for operations that involve multiple models or complex third-party integrations.
4. API Responses (Jbuilder)
- JSON.Jbuilder: Always use
.json.jbuilderfiles for crafting API responses. Do not render JSON directly in controllers. - Decoupling: Keep the response structure separate from the controller logic.
- Explicit Render: If a controller uses
json.jbuilder, always explicitly callrenderat the end of the controller action to show which file is getting rendered (e.g.,render :showorrender 'users/show').