两个阵列的交叉点空出来

Kom*_*mbo 2 ruby ruby-on-rails ruby-on-rails-3

我有两个数组,我试图将它们相交,所以我可以运行一个范围,我的rails代码看起来像:

def index 
    @lists = current_user.lists
    @tasks = @lists.collect{|list| list.tasks}
    @search_tasks = Task.search(params[:search])
    @user_tasks = (@tasks & @search_tasks).now_high
end
Run Code Online (Sandbox Code Playgroud)

在我的浏览器中,我收到以下错误:

未定义的方法`now_high'表示[]:Array

所以这就是我的范围:

scope :now_high, lambda { where("due_date < ? AND importance = ? AND complete = ?", Date.today + 2.days, 'high', false) }
Run Code Online (Sandbox Code Playgroud)

当我说,只在Task.now_high上执行时,这个范围有效 - 我认为问题是我的两个数组相交,我不知道为什么它每次都是空的.

在控制台中:

current_user = User.first
 => #<User id: 1, name:...
@lists = current_user.lists
 => [#<List id: 1, name: "Working on sharing lists"... #gets the lists that belong to the user
@tasks = @lists.collect{|list| list.tasks}
 => [[#<Task id: 1, name: "Add create permissions to 'share'",... #gets a collection of all the tasks on the user's lists.
@search_tasks = Task.all
 => [#<Task id: 1, name: "Add create permissions to 'share'"... #simulating a blank search, we have multiple records in common
ruby-1.9.2-p0 > @tasks & @search_tasks
 => [] #why is this empty?
Run Code Online (Sandbox Code Playgroud)

基本上,只有当数组相交不为空时,我才能有条件地运行我的作用域.我不知道为什么虽然它无论如何都会变空?

luc*_*tte 5

你的@tasks看起来像一个数组数组.尝试在交叉前弄平它.