

Also from Kynth Studios


Also from Kynth Studios


Also from Kynth Studios
123456# Rails Models and Active Record Guide78## Core Philosophy910- Models are the heart of your Rails application11- Keep business logic and data integrity in models12- Use database constraints to enforce rules13- Design for clarity and maintainability14- Trust Active Record's conventions1516## Model Design Principles1718- Focus models on their core domain responsibility19- Keep models cohesive and loosely coupled20- Use descriptive names that reflect business concepts21- Avoid God objects - split large models when needed22- Document complex business rules clearly2324## Schema Management2526- Always use the annotate gem for schema documentation27- Keep schema annotations at the top of model files28- Run annotate after every migration29- Review schema changes carefully before committing30- Document non-obvious column purposes3132## Model Organization3334- Order model contents consistently for readability35- Place constants at the top36- Group related elements together37- Keep public interface minimal38- Document complex logic thoroughly3940## Associations Best Practices4142- Define all relationships explicitly43- Use appropriate dependent options44- Consider bi-directional associations carefully45- Avoid circular dependencies46- Document association purposes when not obvious4748## Validation Strategy4950- Validate at the model level always51- Use database constraints to match validations52- Keep validation messages user-friendly53- Group related validations together54- Test all validation scenarios5556## Database Constraints5758- Add NOT NULL constraints for required fields59- Use unique indexes for uniqueness validations60- Implement foreign key constraints61- Add check constraints for complex rules62- Document constraint purposes in migrations6364## Scopes and Queries6566- Create scopes for commonly used queries67- Keep scope names descriptive and intention-revealing68- Chain scopes for complex queries69- Avoid scopes with side effects70- Use class methods for scopes with parameters7172## Callbacks Guidelines7374- Use callbacks sparingly and purposefully75- Keep callback methods private76- Avoid callbacks that touch other models77- Document callback purposes clearly78- Consider service objects for complex workflows7980## Performance Considerations8182- Always index foreign keys83- Add indexes for frequently queried columns84- Use counter caches for association counts85- Implement includes to avoid N+1 queries86- Profile queries before optimizing8788## Concerns and Modules8990- Extract shared behavior into concerns91- Keep concerns focused on single responsibilities92- Name concerns based on the behavior they provide93- Document concern dependencies94- Test concerns thoroughly9596## Enums and Constants9798- Use enums for status fields with fixed values99- Define constants for magic numbers100- Keep enum values meaningful101- Document enum state transitions102- Consider state machines for complex workflows103104## Data Integrity105106- Implement validations that match business rules107- Use transactions for multi-step operations108- Handle race conditions appropriately109- Validate data consistency regularly110- Plan for data migration scenarios111112## Testing Models113114- Test all validations thoroughly115- Verify association behavior116- Test scopes with various data sets117- Check callback effects118- Ensure data integrity in edge cases119120## Migration Best Practices121122- Keep migrations focused and reversible123- Add indexes in the same migration as columns124- Use strong data types125- Document migration purposes126- Test migrations up and down127128## Active Record Patterns129130- Use find_or_create_by for idempotent operations131- Implement soft deletes when audit trails needed132- Use optimistic locking for concurrent updates133- Leverage Active Record's built-in features134- Avoid raw SQL unless necessary135136## Model Security137138- Never trust user input directly139- Use parameterized queries always140- Implement attribute protection appropriately141- Audit sensitive data access142- Encrypt sensitive attributes143144## Documentation Standards145146- Document complex business logic147- Explain non-obvious validations148- Describe association purposes149- Note performance considerations150- Include usage examples for complex methods151152## Common Anti-Patterns153154- Avoid callbacks that send emails or call APIs155- Don't put view logic in models156- Prevent models from knowing about controllers157- Skip validations only when absolutely necessary158- Don't bypass Active Record without good reason159160## Refactoring Guidelines161162- Extract complex queries to scopes163- Move multi-model logic to service objects164- Split large models along domain boundaries165- Keep methods small and focused166- Maintain backward compatibility167168## Best Practices Summary169170- Trust Active Record conventions171- Keep models focused on data and business rules172- Use database constraints for data integrity173- Write comprehensive tests174- Document complex logic clearly175176Remember: Models are the foundation of your Rails application. Keep them clean, well-tested, and focused on their core responsibilities.177
One repository carrying more than one format is the comparison this product exists for: does anyone actually write different content in each file, or is one a copy of the other?
| Repository | Format | Stack | Covers | Score | Changed |
|---|---|---|---|---|---|
| levifig/rails-instructions.github/instructions/rails-controllers.instructions.md · 54 | Copilot instructions | teststylesecurityapi+1 | 56/100 | 14 days ago | |
| levifig/rails-instructions.cursor/rules/rails-rules.mdc · 54 | Cursor rules | testlint-formatstylearch+9 | 84/100 | 14 days ago | |
| levifig/rails-instructions.github/copilot-instructions.md · 54 | Copilot instructions | teststyletesting-strategysecurity+4 | 71/100 | 14 days ago | |
| levifig/rails-instructions.github/instructions/rails-api.instructions.md · 54 | Copilot instructions | testlint-formatstyletesting-strategy+7 | 67/100 | 14 days ago | |
| levifig/rails-instructions.github/instructions/rails-services.instructions.md · 54 | Copilot instructions | teststylearchtesting-strategy+4 | 75/100 | 14 days ago | |
| levifig/rails-instructions.github/instructions/rails-background-jobs.instructions.md · 54 | Copilot instructions | teststyletesting-strategydatabase+2 | 63/100 | 14 days ago | |
| levifig/rails-instructions.github/instructions/rails-core.instructions.md · 54 | Copilot instructions | teststylearchtesting-strategy+5 | 68/100 | 14 days ago | |
| levifig/rails-instructions.github/instructions/rails-deployment.instructions.md · 54 | Copilot instructions | setupstylesecuritydatabase+2 | 52/100 | 14 days ago | |
| levifig/rails-instructions.github/instructions/rails-hotwire.instructions.md · 54 | Copilot instructions | teststylesecurityperformance+1 | 56/100 | 14 days ago | |
| levifig/rails-instructions.github/instructions/rails-importmaps.instructions.md · 54 | Copilot instructions | teststylesecuritydatabase+2 | 56/100 | 14 days ago | |
| levifig/rails-instructions.github/instructions/rails-mobile.instructions.md · 54 | Copilot instructions | styleuiperformance | 66/100 | 14 days ago | |
| levifig/rails-instructions.github/instructions/rails-performance.instructions.md · 54 | Copilot instructions | teststylegitdatabase+2 | 56/100 | 14 days ago | |
| levifig/rails-instructions.github/instructions/rails-security.instructions.md · 54 | Copilot instructions | teststylesecuritydependencies+3 | 63/100 | 14 days ago | |
| levifig/rails-instructions.github/instructions/rails-styling.instructions.md · 54 | Copilot instructions | styledatabaseuiperformance+3 | 59/100 | 14 days ago | |
| levifig/rails-instructions.github/instructions/rails-testing.instructions.md · 54 | Copilot instructions | teststyletesting-strategysecurity+2 | 56/100 | 14 days ago | |
| levifig/rails-instructions.github/instructions/rails-views.instructions.md · 54 | Copilot instructions | teststylearchsecurity+3 | 60/100 | 14 days ago | |
| levifig/rails-instructions.github/instructions/rails.instructions.md · 54 | Copilot instructions | testlint-formatstylearch+9 | 84/100 | 14 days ago | |
| levifig/rails-instructions.cursor/rules/guidelines.mdc · 54 | Cursor rules | teststyletesting-strategysecurity+4 | 71/100 | 14 days ago |
A badge carrying the measured quality of the strongest agent config file in this repository, out of 100. It reads from this index every time somebody loads your page, so it changes when the measurement changes and there is nothing to keep up to date. Free, no account, and the value is not something you or we can set by hand.
[](https://rulestack.kynth.studio/configs/levifig-rails-instructions-github-instructions-rails-models-instructions)Would rather not hotlink us? Every badge is also served in shields.io’s endpoint schema, so shields renders the image and your readers never talk to our domain:
Published by Toolproof, the masthead over this index and eight others. The method behind the number is at toolproof.kynth.studio/methodology, and the whole thing is readable as JSON with no key at /api.