Rails 使用变量查找条件
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您拥有
@active_subscriptions
,您可以将所有 id 收集到一个数组中,并将它们直接传递到本地订阅模型的find
方法中。我不知道你在这里使用什么属性,所以我只是做一些猜测:One you have the
@active_subscriptions
you can collect all of the ids into an array and pass them right into thefind
method of your local Subscription model. I don't know what attributes you are using here, so I'm just making some guesses:我不确定 Braintree::Subscription.search 返回什么,但如果它类似于 ActiveRecords,您可以使用类似的东西:
.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:
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)