我有两个功能:1.喜欢别针2.关注用户
现在我想列出我关注的用户喜欢的pin。
我在我的用户控制器中有一个方法来列出我关注的用户创建的引脚:
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
数据库中我喜欢的表(由充当votable gem的程序生成):
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
我的pin型号:
class Pin < ActiveRecord::Base
acts_as_votable
belongs_to :user
end
所以我可以从我关注的用户那里创建一个引脚列表,或者一个我喜欢的引脚列表。现在,我想列出我关注的用户喜欢的pin。有人知道怎么做吗?
我已经尝试将其添加到我的控制器中
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)
应该是这样的:
def feeds
@pins = Like.where(liker: current_user.followings)
end