开发者

Intersection of two arrays coming up empty

开发者 https://www.devze.com 2023-03-31 14:38 出处:网络
I have two arrays and I\'m trying to intersect them so I can run a scope, my rails code looks like: def index

I have two arrays and I'm trying to intersect them so I can run a scope, my rails code looks like:

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

In my browser I'm getting the following error:

undefined method `now_high' for []:Array

So here's what my scope looks like:

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

This scope works when I'm say, doing it just on Task.now_high - the problem I think is my intersect of the two arrays, and I don't know why it's coming up empty everytime.

In the console:

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?

Basically, I could conditionally have it run my scopes开发者_如何学编程 only if the array intersect isn't empty. I don't know why though it's coming up empty no matter what?


Your @tasks looks like an array of arrays. Try to flatten it before intersection.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号