avatarpatrykrogala.dev

Summarize

Don’t complicate ActiveRecord. Save it for the database operations.

ActiveRecord act as models for database tables, and the model layer in Rails apps organizes all data your app uses. This includes data not from a database, which you can manage using Active Model.

ActiveRecord = Database Access

Active Record in Rails offers easy database access with just a couple of lines of code, enabling querying and data manipulation like a record.

It’s a key feature that boosts developer productivity. Typically, if you keep business logic separate, you’ll need minimal code in your Active Records, just enough for full data access. Sometimes, you may need to add code for configurations (like belongs_to or validates), class methods for database queries to avoid code duplication, or instance methods for defining core attributes derived directly from the database.

Use Class Methods for Common DB Operations

Rails class methods typically help access the database, and you should only add new methods for reuse. Only add class methods to your Active Record models when:

  1. The same logic is needed in multiple places.
  2. The logic only involves database manipulation, not business logic.

For example, consider a scenario with users having statuses: “fresh”, “confirmed”, and “banned”. If you frequently query for banned users, instead of repeatedly writing User.where(status: "banned"), you can define a class method:

Now, you can simply call User.banned to get banned users without worrying about the specific database string.

However, if your logic includes business decisions, like finding users confirmed in the last 10 days, this involves business logic and should not be a class method in the model. This kind of logic, being more specific and potentially changeable, fits better in a service layer, keeping your models clean and focused on database interactions.

Use Instance Methods for Domain Concepts

The same principles from the previous section apply here as well: avoid putting business logic or logic related to derived data as instance methods in Active Record models. Instead, instance methods should represent strongly-defined domain concepts that can be directly derived from database data without additional logic.

For example, we can use a method user_facing_identifier for a formatted ID of user (something like Discord perhaps?), which is a part of our domain because users interact with it directly. This method is justified as an instance method because it is a clear domain concept, directly derivable from the database:

This approach keeps our models clean and focused on the domain, avoiding the mix of business logic, presentation logic, and use-case-specific calculations.

For something like displaying the first letter of a users name for aesthetic reasons in the view, which does not belong to the domain logic, we should not add a method in the User model.

Active Model is for Modeling

Active Model allows us to use objects that act like Active Records, complete with Rails form and URL helper compatibility.

How to Create the Model with Active Model: Include ActiveModel::Model in your class and use attr_accessor to define attributes. This setup provides a constructor accepting a hash of attributes and enables bulk assignment.

Ensure Rails Compatibility: Implement to_key and persisted? methods to integrate with Rails form and URL helpers. to_key returns an array of values that uniquely identify an instance, while persisted? should return true to mimic an Active Record’s behavior.

With these implementations, objects can be used in Rails views similarly to Active Record instances, supporting routes and form helpers without requiring a database-backed model. This simplifies the creation of custom, lightweight data objects within Rails applications, using Active Model’s capabilities.

Want to learn about view tests in Rails Check out awesome book by David Bryant Copeland Sustainable Web Development with Ruby on Rails: Practical Tips for Building Web Applications that Last

Hey! Check out my links!

Ruby on Rails
Activerecord
Programming
Web Development
Software Development
Recommended from ReadMedium