如何调用锻炼.user_id == current_user.id 的帖子(在本例中为锻炼)的所有评论?

发布于 2024-10-05 02:32:14 字数 857 浏览 6 评论 0原文

我创建了一个 Ruby on Rails 应用程序,用户可以在其中记录他们的锻炼,其他用户可以对这些锻炼发表评论。我正在使用仪表板资源来聚合 current_user 的信息。我正在尝试显示有关 current_user 锻炼的最新评论,但似乎无法弄清楚如何正确执行此操作。我想我需要一个我还不擅长的named_scope。

我本质上希望应用程序循环浏览评论表,但只返回有关“锻炼”的评论,其中“workout.user_id ==”到“current_user.id”。

/views/dashboard/index.html.erb

<% @comments.each do |comment| %> 
  <%= link_to (comment.user.username), comment.user %><br/>
  <%= time_ago_in_words(comment.created_at) %><br/>
  <%= link_to (comment.workout.title), comment.workout %><br/>
<% end %>

dashboard_controller.rb

def index
  @comments = Comment.all(:order => "created_at DESC", :limit => 10)
  @workouts = Workout.all(:order => "created_at DESC", :limit => 10) 
end 

*我认为我不需要在其中添加 @workouts 行,但无论如何都要放它。

I have created a Ruby on Rails app where users can record their workouts and other users can comment on those workouts. I am using a Dashboard resource to aggregate information for current_user. I am trying to display recent comments on a current_user's workouts but can't seem to figure out how to do this correctly. I think I need a named_scope which I am not great at yet.

I essentially want the app to loop through the comments table but only return comments on Workouts where workout.user_id == to current_user.id.

/views/dashboard/index.html.erb

<% @comments.each do |comment| %> 
  <%= link_to (comment.user.username), comment.user %><br/>
  <%= time_ago_in_words(comment.created_at) %><br/>
  <%= link_to (comment.workout.title), comment.workout %><br/>
<% end %>

dashboard_controller.rb

def index
  @comments = Comment.all(:order => "created_at DESC", :limit => 10)
  @workouts = Workout.all(:order => "created_at DESC", :limit => 10) 
end 

*I don't think I need the @workouts line in their but put it anyway.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灯下孤影 2024-10-12 02:32:14

假设您已正确设置模型,您可以尝试以下操作:

class Comment < ActiveRecord::Base
  named_scope :for_user, lambda { |user| { :joins => :workout, :conditions => ["workouts.user_id = ?", user.id] } }
  named_scope :order, lambda { |order| { :order => order } }
  named_scope :limit, lambda { |limit| { :limit => limit } }
end

class DashboardsController < ApplicationController
  def index
    @comments = Comment.for_user(current_user).order("created_at DESC").limit(10)
  end
end

Assuming that you have the models setup properly, here's something you can try:

class Comment < ActiveRecord::Base
  named_scope :for_user, lambda { |user| { :joins => :workout, :conditions => ["workouts.user_id = ?", user.id] } }
  named_scope :order, lambda { |order| { :order => order } }
  named_scope :limit, lambda { |limit| { :limit => limit } }
end

class DashboardsController < ApplicationController
  def index
    @comments = Comment.for_user(current_user).order("created_at DESC").limit(10)
  end
end
乖乖公主 2024-10-12 02:32:14

正确的方法是在模型之间建立关系,即评论和锻炼。每个锻炼可以有许多评论,每个评论都属于一个锻炼。所以:

class Comment < ActiveRecord::Base
      belongs_to :workout

      # rest of the model definitions
end

一旦

class Workout < ActiveRecord::Base
      has_many :comments

      # rest of the model definitions
end

你像这样设置它,那么你就可以调用:

<% @workout.comments do |comment| %> 
   <!-- print comment here -->
<% end %>

你可以按照示例

The proper way to do is to put relations between your models, i.e. comment and workout. Each workout can have many comments, and each comment belongs to a workout. So:

class Comment < ActiveRecord::Base
      belongs_to :workout

      # rest of the model definitions
end

and

class Workout < ActiveRecord::Base
      has_many :comments

      # rest of the model definitions
end

Once you set it up like this, then you can call:

<% @workout.comments do |comment| %> 
   <!-- print comment here -->
<% end %>

You can follow the example here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文