Rails 使用变量查找条件

发布于 2024-12-11 16:42:20 字数 465 浏览 0 评论 0原文

我正在使用 Braintree 来管理 Rails 应用程序中的订阅。

我有一个存储 Braintree 客户 ID 和订阅 ID 的订阅模型。

我想在我的订阅模型中过滤活动订阅。到目前为止,我已经有了

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

,但现在我想使用 @active_subscriptions 中的订阅 ID 来查找本地订阅模型中具有相同订阅 ID 的所有对象,并将其放入 @local_active_subscriptions 等变量中。

我必须这样做的原因是使用本地信息访问 Braintree::Address 并且仅提取活动地址。

感谢您的帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恋竹姑娘 2024-12-18 16:42:21

如果您拥有 @active_subscriptions,您可以将所有 id 收集到一个数组中,并将它们直接传递到本地订阅模型的 find 方法中。我不知道你在这里使用什么属性,所以我只是做一些猜测:

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

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)
椒妓 2024-12-18 16:42:21

我不确定 Braintree::Subscription.search 返回什么,但如果它类似于 ActiveRecords,您可以使用类似的东西:

@local_active_subscriptions = Subscription.where("id IN(?)", @active_subscriptions.map{ |act_subs| act_subs.id })

.map 函数应该将所有 ID 放入一个数组中,然后 ActiveRecord 查询将查找所有订阅表中 ID 位于该数组中的订阅。
我不确定 Braintree::Subscriptions 上的映射;我从来没有用过那个。

编辑

就像ctcherry所说,您也可以使用find进行搜索。我想收集也适合将 id 映射到数组中。您也可以使用 @active_subscriptions.map(&:id)

I'm not sure what Braintree::Subscription.search returns, but if it's something akin to ActiveRecords, could you use something like:

@local_active_subscriptions = Subscription.where("id IN(?)", @active_subscriptions.map{ |act_subs| act_subs.id })

The .map function should put all the IDs into an array, and then ActiveRecord query would look for all the Subscriptions in your subscriptions table whose ID is in that array.
I'm not certain about mapping on Braintree::Subscriptions; I've never used that.

Edit

Like ctcherry said, you can also do the search with find. And I guess collect is good for mapping the ids into an array too. You could also maybe use @active_subscriptions.map(&:id)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文