Rails数组从数组中拒绝

时间:2014-10-18 19:58:55

标签: ruby-on-rails arrays

我很难搞清楚这一个。在我的索引页面上,我有一个隐藏投票卡的开关。 kaminari paginate gem的工作方式,如果您已经投票选出前10张卡,则第1页显示的结果将少于10张卡。所以,如果存在param,我正试图删除用户投票的卡片。

如何设置@ cards.reject以处理要删除的项目数组?

cards_controller.rb

@cards = Card.all
@card_total = @cards.count
@three_pics = @cards.sample(3, random: 1)
@new_cards = @cards.last(5).reverse!
if params[:voted] == 'on' then
  @voted = Vote.where(:user_id => current_user.id).select(:card_id)
  @cards = @cards.reject { |item| block }
end
if params[:view] == 'new' then
  @cards = @cards.order(created_at: :desc).page(params[:limit]) if params[:limit]
elsif params[:view] == 'old' then
  @cards = @cards.order(created_at: :asc)
elsif params[:view] == 'least_pop' then
  @cards = @cards.order(popularity: :asc, created_at: :asc)
else
  @cards = @cards.order(popularity: :desc, created_at: :desc)
end

更新

让我详细说明一个例子和一些更新的代码

@voted_did = Vote.where(:user_id => current_user.id).pluck(:card_id)

@voted = [1,3,8]

@cards = [{“id”=>“1”,“event”=>“有些事件”,{“id”=>“2”,“event”=>“有些事件” ,........},{“id”=>“3”,“event”=>“Some Event”,........},{“id”=> “4”,“event”=>“Some Event”,........},{“id”=>“5”,“event”=>“Some Event”,... .....},{“id”=>“5”,“event”=>“Some Event”,........},{“id”=>“7”, “event”=>“Some Event”,........},{“id”=>“6”,“event”=>“Some event”,....... 。},{“id”=>“9”,“event”=>“有些事件”,........}}

我试图找出如何不仅从@cards中删除id,而是删除该id的整个数组,如果它在@voted中找到

1 个答案:

答案 0 :(得分:0)

如何选择未经用户投票的卡?

喜欢:@voted = Vote.where.not(user_id: current_user.id)

顺便说一下,你从未真正发送过块:

@cards.reject { |item| block }

块部分应该是逻辑;)