列出我关注的用户的项目

时间:2015-04-12 20:46:12

标签: ruby-on-rails

我有两个功能: 1.喜欢别针 2.关注用户

现在我要列出我关注的用户喜欢的引脚。

我的用户控制器中有一个方法列出我所关注的用户创建的

def feeds
        @pins =  Pin.where(user_id: current_user.followings.pluck(:id))
end

现在我想创建一个我关注的用户喜欢的图钉列表。我使用这些行为作为可选择的宝石喜欢引脚,所以我的引脚控制器有:

def like
    @pin.liked_by current_user
    redirect_to :back, notice: 'You have added this restaurant to your favorite list.'
    ModelMailer.new_like_notification(@pin).deliver
  end

  def unlike
    @pin.unliked_by current_user
    redirect_to :back, notice: 'You have removed this restaurant from your favorite list.'
  end

要列出当前用户自己喜欢的引脚,我创建了:

def my_favorites
    @pins = current_user.find_liked_items
  end

我喜欢数据库中的表(由作为可投票宝石的行为生成):

create_table "likes", force: true do |t|
    t.string   "liker_type"
    t.integer  "liker_id"
    t.string   "likeable_type"
    t.integer  "likeable_id"
    t.datetime "created_at"
  end

  add_index "likes", ["likeable_id", "likeable_type"], name: "fk_likeables"
  add_index "likes", ["liker_id", "liker_type"], name: "fk_likes"

我的下表:

create_table "follows", force: true do |t|
        t.integer  "user_id"
        t.integer  "followable_id"
        t.string   "followable_type"
        t.datetime "created_at"
        t.datetime "updated_at"
      end

  add_index "follows", ["followable_id", "followable_type"], name: "index_follows_on_followable_id_and_followable_type"
  add_index "follows", ["user_id", "followable_id", "followable_type"], name: "index_follows_on_user_id_and_followable_id_and_followable_type", unique: true

我的销钉型号:

class Pin < ActiveRecord::Base
    acts_as_votable
    belongs_to :user
end

所以我可以创建一个我正在关注的用户的引脚列表,或者我喜欢的引脚列表。现在,我想列出我关注的用户喜欢的引脚。有谁知道怎么做?


我试图将此添加到我的控制器

def feeds 
  @pins = Like.where(liker: current_user.followings.pluck(:id)) 
end 

但是我收到以下错误:

SQLite3::SQLException: no such column: likes.liker: SELECT "likes".* FROM "likes" WHERE "likes"."liker" IN (1, 2, 3, 5, 6, 7, 8, 9, 10) 

1 个答案:

答案 0 :(得分:0)

它应该是这样的:

def feeds 
  @pins = Like.where(liker: current_user.followings) 
end