I have an action that simply toggles the
@blog.active == true
@blog.active == false
class BlogsController < ApplicationController
There are two things here contributing to that sense of ugh you're getting from this piece of code:
activeboolean”) does not match the elegance and brevity of the code.
The solution to both is a named method. I think it makes sense on the model, like this:
class Blog < ApplicationRecord def toggle_active! update active: !active end end # controller @blog = Blog.find(1) @blog.toggle_active!
It's now clear in both the model and the controller what is occurring because the name/label,
toggle_active, is there to add that context and clarity. Further, the controller doesn't need to know too much about the implementation of the method, which allows
Blog to change how that occurs in the future should it be necessary.