What is the best way to reuse a scope in Rails?

Viewed 38

I'm confused to reuse or writing a new scope. for example,

one of my methods will return future subscription or current subscription or sidekiq created subscriptions.

as scopes will look like:

scope :current_subscription, lambda {
    where('(? between from_date and to_date) and (? between from_time and to_time)', Time.now, Time.now)
  }

  scope :sidekiq_created_subscription, lambda {
    where.not(id: current_subscription).where("(meta->'special_sub_enqueued_at') is not null")
  }

  scope :future_subscription, lambda {
    where.not(id: current_subscription).where("(meta->'special_sub_enqueued_at') is null")
  }    

so these were used for separate purposes in different methods, so for me what I tried is to check whether a particular account record will come under which of three subscriptions.

so I tried like:

def find_account_status
    accounts = User.accounts
    name = 'future' if accounts.future_subscription.where(id: @account.id).any? 
    name = 'ongoing' if accounts.current_subscription.where(id: @account.id).any? 
    name = 'sidekiq' if accounts.sidekiq_enqued_subscription.where(id: @account.id).any? 
return name
    end

so here what my doubt is, whether using like this is a good way, as here we will be fetching the records based on the particular subscriptions and then we are checking whether ours is there or not.

can anyone suggest any better way to achieve this?

0 Answers
Related