开发者

How do I find the union of two arrays of models by model attribute in Rails 3?

I have an array of the current_user's FB friends and another of other users that belong to a given event.

I need to find all of the current_user's FB friends that also belong to the event.

Tried this in my events_controller:

@friends = Array.new
@the_others = Array.new
@event.users.e开发者_C百科ach do |user|
  @fb_user.friends.each do |friend|
    if friend.identifier == user.uid
      @friends << user
    else
      @the_others << user
    end
  end
end

Clearly this is way off...in fact it's not working...someone please set me straight :-)


Try this

@friends = Array.new
@the_others = Array.new
@event.users.each do |user|
   if @fb_user.friends.detect{|friend| friend.identifier == user.uid}
     @friends << user
   else
     @the_others << user
   end
end


@friends = [1,2]
@the_others = [2,3,4]
union = @friends | @the_others
 => [1, 2, 3, 4] 
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜