Don't Repeat Yourself (DRY)
suggest changeRails favors DRY code: reuse a piece of logic instead of copying it (for example, with partials). One copy is easier to fix, so the code stays smaller and more consistent.
Also Fat Model, Skinny Controller is DRY, because you write the code in your model and in the controller only do the call, like:
# Post model
scope :unpublished, ->(timestamp = Time.current) { where("published_at IS NULL OR published_at > ?", timestamp) }
# Any controller
def index
# ...
@unpublished_posts = Post.unpublished
# ...
end
def others
# ...
@unpublished_posts = Post.unpublished
# ...
end
This also helps lead to an API driven structure where internal methods are hidden and changes are achieved through passing parameters in an API fashion.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents