如何获取不在X列表中的随机ID

时间:2015-01-07 05:26:58

标签: ruby-on-rails

所以我有一个代码片段,它基本上可以找到一个随机List,并将其打印出来。但是在循环中它还将该id保存到名为status的表中。

现在我希望能够再次查看该列表,这次打印出150个randoms。但是,这次我希望它首先检查状态,以确保之前没有打印出List项。

这是我现在的代码:

class ScheduleTweets
@queue = :schedules_queue

def self.perform(user_token, user_id)
client = Buffer::Client.new(user_token)
user = user_id
list = List.all.sample(150)
profiles = client.profiles
profile_ids = profiles.map(&:id)
list.each do |list|
  list.statuses.create(:user_id => user)
    client.create_update(body: {text: "#{list.text}", profile_ids: profile_ids })
  end
 end
end

如果我猜测我应该在List.all.sample(150)之后添加一些内容,它会检查列表中是否有状态中存在的list_id。

1 个答案:

答案 0 :(得分:0)

statues表中收集列表项并创建一个包含该结果的数组。

stat_ids = Status.all.collect{|s| s.list_id}

现在遍历随机150列表并检查 stat_ids 中是否包含列表ID。

random_lists.each do |rl|
  if stat_ids.include?(rl.id)
    //do something
  end
end

由于我不知道您的数据库架构,我假设它如上所述。