Model Methods & Scopes
Master Ruby on Rails Model Methods and Scopes
Core Ruby on Rails Concepts
Model Methods
Custom behaviors added to models for data processing and business logic. Help keep controllers thin and models fat.
Scopes
Predefined database queries that filter records based on specific patterns. Enable reusable query logic.
Fat Model, Skinny Controller
Rails principle advocating for business logic in models rather than controllers for better code reusability.
Prerequisites Setup Process
Navigate to Project Directory
Open Terminal and navigate to your Rails class folder using cd command with drag-and-drop functionality
Clone Repository
Run Git clone command to copy the Flix repository from Bitbucket for the exercise foundation
Install Dependencies
Execute bundle and yarn install commands to set up all required gems and JavaScript dependencies
When Rails divides using the forward slash operator, it completely discards the remainder. Use the modulus operator (%) to capture only the remainder for minutes calculation.
Runtime Display Comparison
| Feature | Before | After |
|---|---|---|
| Display Format | 238 minutes | 3 hrs. 58 min. |
| User Friendliness | Requires calculation | Immediately readable |
| Code Location | View template | Model method |
Use class methods (with self) when data is common to all instances. The MPAA ratings list is the same for every movie, making it perfect for a class method invoked as Movie.all_mpaa_ratings.
Ruby Array Syntax Options
Traditional Syntax
Standard array notation using square brackets and quoted strings: ['G', 'PG', 'R', 'NR']
Word Array Syntax
Simplified Ruby syntax using %w for simple strings without spaces: %w(G PG R NR)
Implementing Scope Functionality
Define Routes
Add route definition in routes.rb to handle placement parameters for movie categorization
Create Controller Action
Build recommended method in movies controller with case statement for different placement values
Write Scope Definitions
Define scopes in movie model using lambda syntax and where clauses for database filtering
Update View Templates
Modify index view to include functional navigation links using Rails link_to helper
The view expects an @movies instance variable containing movie objects. It doesn't care whether @movies contains all movies or a subset - the controller decides which collection to display. This demonstrates MVC working in harmony.
Scope Refactoring Benefits
| Feature | Original Approach | DRY Approach |
|---|---|---|
| Number of Scopes | 3 separate scopes | 1 parameterized scope |
| Code Duplication | High repetition | Minimal repetition |
| Maintainability | Multiple update points | Single update point |
| Flexibility | Fixed placement values | Any placement value |
The syntax -> (placement) { where(placement: placement) } creates a parameterized scope that accepts variables. This Rails-specific syntax enables dynamic filtering while maintaining the scope interface.
Implementing Scope Functionality
Define Routes
Add route definition in routes.rb to handle placement parameters for movie categorization
Create Controller Action
Build recommended method in movies controller with case statement for different placement values
Write Scope Definitions
Define scopes in movie model using lambda syntax and where clauses for database filtering
Update View Templates
Modify index view to include functional navigation links using Rails link_to helper
The view expects an @movies instance variable containing movie objects. It doesn't care whether @movies contains all movies or a subset - the controller decides which collection to display. This demonstrates MVC working in harmony.
Scope Refactoring Benefits
| Feature | Original Approach | DRY Approach |
|---|---|---|
| Number of Scopes | 3 separate scopes | 1 parameterized scope |
| Code Duplication | High repetition | Minimal repetition |
| Maintainability | Multiple update points | Single update point |
| Flexibility | Fixed placement values | Any placement value |
The syntax -> (placement) { where(placement: placement) } creates a parameterized scope that accepts variables. This Rails-specific syntax enables dynamic filtering while maintaining the scope interface.
Key Takeaways
