Rails4充当的有投票权的检索记录?投票吗?项 - Rails 4 acts as votable retrieving record of the “voted” item

- 此内容更新于:2016-02-01
主题:

我能显示投票用户喜欢,但我不知道如何显示的实际记录喜欢记录。最简单的方法来检索用户投票记录什么?控制器。rbshow.html。haml结果:我怎么把这个所以我可以只显示标题,描述,等等……从帖子记录?

原文:

I'm able to display the votes user likes, but I don't know how to display the actual record of the liked record. Whats the simplest way to retrieve user voted records?

controller.rb

def show
  @user = User.find(params[:id])
  @liked = @user.votes
end

show.html.haml

= @liked.to_json

Result:

[{"id":5,"votable_id":8,"votable_type":"Post","voter_id":2,"voter_type":"User","vote_flag":true,"vote_scope":null,"vote_weight":1,"created_at":"2016-02-01T00:33:15.968Z","updated_at":"2016-02-01T00:33:15.968Z"}]

How do I turn this so I can just display the title, description, etc... from the Post record?

解决方案:
投票有post_id吗?我以为你有投票。rb和职位。rb模型。投票。rb职位。rb表演
原文:

Does the vote have post_id ? I assumed you have vote.rb and post.rb model.

vote.rb

belongs_to :post

post.rb

has_many :votes

In show

- @liked.each do |l| 
  = l.post.title 
  = l.post.description 
楼主:不,我使用acts_as_votable宝石,所以他们没有post_id

(原文:No, I'm using acts_as_votable gem, so they don't have post_id)

网友:好吧,你可以添加一个额外的post_id列使用迁移。stackoverflow.com/a/4834906/3396429

(原文:Well, you can just add an extra post_id column using migration. stackoverflow.com/a/4834906/3396429)

楼主:但不是重点的多态关联所以你不必post_id列吗?

(原文:But isn't the whole point of having the polymorphic association so you don't have to have the post_id column?)

网友:我以为votable_id持有用户id和postid..你试着用@user.votable吗

(原文:I assumed votable_id hold user id and post id.. Have you try using @user.votable)