我的應用程序有簡單的用戶/帖子/投票結構,用戶可以在每個帖子上進行投票/反對投票。我想讓用戶在索引頁面查看「他們自己的未投票帖子」,我試過where
where.not
,但無法將它合併起來。我的Rails應用讓用戶在帖子上投票,如何讓用戶在索引頁面看到「他自己的未投票」帖子?
我試圖在posts_controller
中這樣做,我最好的辦法是找到所有未投票的帖子,但不知道如何獲得與用戶的相關信息。
def index
if logged_in?
@posts = Post.where.not(id: Vote.all.map(&:voteable_id))
else
@posts = Post.all.sort_by{|x| x.total_votes}.reverse
end
末
這次投票是多態的關聯,下面是3種型號:
Post: has_many :votes, as: :voteable,
Vote: belongs_to :voteable, polymorphic: true,
User: has_many :votes
我logged_in?
判斷是否存在@current_user
。
這裏是我的回購:https://github.com/Tim-Feng/ad-judge
用戶如何與投票相關?顯示您的模型關聯代碼,並且可能會更容易幫助您更新查詢。 –
您好@ChrisPeters,我在投票模型上使用多態關聯,下面是3個模型:'Post:has_many:votes,as::voteable','Vote:belongs_to:voteable,polymorphic:true','User:has_many:votes ' – Disco26