THpubs
THpubs

Reputation: 8172

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...

Upvotes: 0

Views: 112

Answers (1)

Roman Kiselenko
Roman Kiselenko

Reputation: 44370

Create scope:

class MyModel < ActiveRecord::Base
  scope :approved, -> { where(approved: true) } 
end

and use it like:

MyModel.approved

Upvotes: 3

Related Questions