Ruby on rails Rails-连接表以对帖子进行排序

Ruby on rails Rails-连接表以对帖子进行排序,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,在我的rails应用程序中,我有一个表设计,它本质上就是帖子。用户可以喜欢这些设计(使用gem)。这张桌子叫做选票。在用户的个人资料页面上,我展示了他们喜欢(或投票赞成)的所有设计。在用户模型中,我有一个方法: def favorites Design.joins(:votes).where('voter_id = ?', self.id).where('voteable_type = ?', 'Design').where('vote = ?', true) end 然后在用户的控制器中

在我的rails应用程序中,我有一个表设计,它本质上就是帖子。用户可以喜欢这些设计(使用gem)。这张桌子叫做选票。在用户的个人资料页面上,我展示了他们喜欢(或投票赞成)的所有设计。在用户模型中,我有一个方法:

def favorites
  Design.joins(:votes).where('voter_id = ?', self.id).where('voteable_type = ?', 'Design').where('vote = ?', true)
end
然后在用户的控制器中调用这些设计

def show
  @designs = @user.favorites
end
这显示了他们喜欢的所有设计,但这是按照设计创建的时间顺序,而不是投票创建的时间顺序。投票表中有一个created_at列,所以我知道我可以根据他们喜欢的时间对这些设计进行排序

我试过了,但运气不好

def favorites
  results = Design.joins(:votes).where('voter_id = ?', self.id).where('voteable_type = ?', 'Design').where('vote = ?', true)
  results.sort! {|t1, t2| t2.vote.created_at <=> t1.vote.created_at}
end

谢谢

我认为您可以为此使用a-我会在
用户
模型上使用
范围
与您的
用户
实例中的投票模型进行通信:

#app/models/user.rb
Class User < ActiveRecord::Base
    has_many :votes, as :voteable
    has_many :designs, through: :votes

    scope :favorites, -> { joins(:votes).where('voter_id = ? AND votable_type = ? AND vote = ?', id, 'Design', true) }
    scope :latest, -> { order(created_at: :asc) }
    scope :latest_favorites, -> { favorites.order(created_at: :asc) }
end

前面的答案不起作用,因为作用域在类级别上起作用,所以当您在用户实例上使用它时,它假定
favorites
是一个实例方法或关联

我建议您使用投票模型,并在投票时参考设计协会的成员:

def show
  @votes = @user.votes.includes(:design).order(created_at: :asc)
  @designs = @votes.collect {|v| v.design }
end
您可以随意将其移动到
用户
模型,以代替您的
收藏夹
方法,如下所示:

def favorites
  @favorites ||= self.votes.includes(:design).order(created_at: :asc).collect {|v| v.design }
end

更新 由于您正在使用此
thumbs\u up
gem,以下操作将起作用:

行动中

def show
  @designs = @user.votes.where(voteable_type: 'Design').order(created_at: :asc).collect {|v| v.voteable}
end
或方法

def favorites
  @favorites ||= self.votes.where(voteable_type: 'Design').order(created_at: :asc).collect {|v| v.voteable}
end

奇怪的是,我得到了一个
未定义的方法“收藏夹”
,它已经在用户模型中定义了
@User
实例变量是如何定义的?
@User=User.find_by_username(参数[:id])
find\u by\u username是一种动态方法,如果users表中没有username列或用户模型中没有username属性,则该方法无效。检查它是否返回nil。Rails 4中已经弃用了动态查找程序。您应该使用
User.find_by(username:params[:username])
。不过,这里的属性似乎不匹配(使用id按用户名搜索)。您是否尝试过
User.find(params[:id])
?感谢@omarvelous的回复。使用您在我的
用户
模型中提到的第二种方法,在控制器中调用:
@designs=@User.favorites
,我收到的错误
未找到名为“design”的关联;也许你拼错了?
。这个gem的工作方式是在voteable模型中使用
作为voteable
(如果这是相关信息),因为我不确定为什么没有找到关联,除了查找
投票的voteable模型
它存储在一个名为
voteable类型
的字符串中。我快速阅读了插件,您将需要执行以下操作,我更新了上面的答案。哇,非常感谢你花这么多时间来帮助我。我真的很感激!我想你是对的,但是我得到了这个涉及类型关联的错误<代码>未定义的方法“按类型”…我最近读到类型关联再次被弃用。。。奇怪的是,它仍然在宝石自述中。误读你的问题,你在使用宝石!哈哈,好的。完全取决于您,但可能更为最新,还支持Rails 4。
def show
  @designs = @user.votes.where(voteable_type: 'Design').order(created_at: :asc).collect {|v| v.voteable}
end
def favorites
  @favorites ||= self.votes.where(voteable_type: 'Design').order(created_at: :asc).collect {|v| v.voteable}
end