Understanding the Difference Between Business Rules and Ruby on Rails Associations
Snippets of Code is an engineering journal documenting learning for experiments and projects. Please share or like if the content is helpful.
When one object sends a message to another, the receiving object tests how it handles that message.
Testing outgoing query messages from the user's perspective can be repetitive and unnecessary.
Instead of testing the message itself, could you focus on the results it should produce? If a message doesn’t affect anything outside the object that sends it, there’s no need to test it.
Here, `find_posts` is a query method that gets data. We don’t need to test the method itself if it doesn’t change anything outside its object.
Query results are not plain data but come from calculations. Even though they are calculated, they are still part of the query.
When testing associations, you often check if changes in one part of your program cause the proper modifications elsewhere.
For example, if a user is linked to posts, changing the user’s details should also be reflected in related posts.
If you are enjoying the learning journal, please ❤️
In this case, changing a `User` object should update the associated `Post` objects.
Ruby on Rails has built-in tests for these associations. While tests show how things should work, using a Domain-Specific Language (DSL) helps define these rules in the code.
If you think someone will benefit from this content, please share.
It’s best to ensure these associations work at the database level and check them with tests.
Associations are about the application's structure rather than business rules.
Business rules are guidelines that help a company make more money or save costs. They define how the business operates and how it should behave. Associations in code are just technical details about how different parts of your program connect, not business rules.