How to create drafts and publish only if approved in Rails?

I have already built a Rails app which have a model called Product. Currently, I have an attribute named approved to mark the approval of a product. So when listing the products, every-time I need to use a where command to check whether approved is true or false? So my question is…

  • Is there a better way to do this?
  • Can I override the model’s show all method and show only approved ones?
  • Is there a good gem for this?

In your view, do something like this:

<%= if @product.approved? %><em>APPROVED</em><% end %>

Using Ruby’s cool partition command you could separate the collection of Products into two arrays (and put each one in its own instance variable).

Or in your Model, you can simply query for ONLY those Products where approved is true.

instead of using Product.all I’d look at creating some scopes such as Product.approved or the like, which are more what you are wanting.

2 Likes

Thanks! This way seems to be better!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.