Search code examples
ruby-on-railsfindconditional-statementsbraintree

Rails using a variable find condition


I am using Braintree for managing subscriptions in my Rails app.

I have a Subscription model that stores the braintree customer ID and subscription ID.

I want to filter active subscriptions in my Subscription model. So far I have

def find_active_subscriptions
@active_subscriptions = Braintree::Subscription.search do |search|
  search.status.is "Active"
end

But now I want to use the subscription IDs in @active_subscriptions to find all of the objects in my local Subscription model with the same subscription IDs and put that into a variable such as @local_active_subscriptions.

The reason I have to do this is to use the local info to access Braintree::Address and only pull active addresses.

Thanks for the help.


Solution

  • One you have the @active_subscriptions you can collect all of the ids into an array and pass them right into the find method of your local Subscription model. I don't know what attributes you are using here, so I'm just making some guesses:

    @active_subscription_ids = @active_subscriptions.collect(&:subscription_id)
    @local_active_subscriptions = LocalSubscriptionModel.find(@active_subscription_ids)