Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ruby删除公共元素集合_Ruby_Ruby On Rails 4 - Fatal编程技术网

ruby删除公共元素集合

ruby删除公共元素集合,ruby,ruby-on-rails-4,Ruby,Ruby On Rails 4,我有三个类:用户、订阅和计划。我想加载用户没有的所有计划。在Rails中最好的方法是什么 我有两个集合:current\u user.subscriptions和Plan.where(active:true) 我用的是mongoid def dashboard @plans = Plan.where(active: true)#.each { @plans.delete_if has_current_user_plan subscription.title } end def h

我有三个类:用户、订阅和计划。我想加载用户没有的所有计划。在Rails中最好的方法是什么

我有两个集合:
current\u user.subscriptions
Plan.where(active:true)
我用的是mongoid

def dashboard
  @plans = Plan.where(active: true)#.each { @plans.delete_if has_current_user_plan      subscription.title }
end

def has_current_user_plan(name)
  current_user.subscriptions.where(title: name, active: true).exists?
end
AR:


谢谢,实际上我正在使用MongoID;所以我不能这么做。还有其他想法吗?我已经更新了答案。方法是一样的,收集用户的计划id,然后选择每个id不匹配的计划。好的,谢谢;我会检查并让您知道,因为我现在对paypal沙箱有问题:(,我无法完成订阅过程来告诉您它是否工作正常;但至少它编译正常。它工作:)谢谢。还有一件事,你从哪里得到的文件?我正在四处寻找,请粘贴链接。我很高兴听到这个消息。我当时正在查看和挖掘源代码。Github的回购搜索非常简洁。
class User
  has_many :subscriptions
class Subscription
  belongs_to :plan
  belongs_to :user
class Plan
  has_many :subscriptions
class User < ActiveRecord::Base
  has_many :subscriptions
  has_many :plans, through: :subscriptions # !!!
end

Plan.where(active: true).where.not(id: current_user.plans)
Plan.where(active: true).not_in(_id: Subscription.where(user_id: current_user.id).pluck(:plan_id))