From 17b74496f6cb61102f20d4ff4988d3889b952746 Mon Sep 17 00:00:00 2001 From: Igor Zawgorodskiy Date: Sun, 5 May 2019 17:05:34 +0300 Subject: [PATCH] Added some model issues --- guides/models.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/guides/models.md b/guides/models.md index 746b39f..4af0583 100644 --- a/guides/models.md +++ b/guides/models.md @@ -7,6 +7,9 @@ The following is a base template for an ActiveRecord model `Customer`. * Avoid ActiveRecord callbacks where possible, prefer Service Objects architecture to store any business * Order associations, scopes and validations alphabetically * Aim to remove business logic that is not directly related to reading from or writing to the database +* `Scopes` vs `Class methods`. Use scopes when the logic is very small, for simple where/order clauses, + and class methods when it involves a bit more complexity. +* Lets DRY our code. Use special concerns to avoid duplicating. ## Template @@ -77,4 +80,20 @@ class Customer < ActiveRecord::Base # some calculation end end + +# app/models/concerns/commentable +module Commentable + # Concern should be named with `able` prefix + extend ActiveSupport::Concern + # EXTENDing is required + + included do + # For associations + has_many :comments, as: :author, class_name: 'Comment' + end + + def method_name + # Some code + end +end ```