我最喜欢的用户类别有原因。如何从current_user.subscriptions获取所有原因?现在我有代码:
@categories = current_user.subscriptions
@causes = @categories.causes
但它返回
undefined method 'causes' for #<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Category:0x0000000375d700>
答案 0 :(得分:1)
尝试这个简单的解决方案:
@categories = current_user.subscriptions
@causes = Cause.where(category_id: @categories)
答案 1 :(得分:0)
您可以使用此查询
@categories = current_user.includes(:subscriptions => :causes)
它包含与current_user相关的所有类别和原因