简体   繁体   English

在Rails中显示用户的帖子列表

[英]Display a list of user's posts in Rails

I have a rails app where users post reviews of albums. 我有一个Rails应用,用户可以在其中发布相册评论。 The reviews that are posted are called "pins" and are uploaded with a release date, album title, artist name, album cover image, and rank for the year. 发布的评论称为“图钉”,并随发行日期,专辑标题,艺术家姓名,专辑封面图像和年份排名一起上传。 (as in my #1 favorite album of the year) The pins belong users and the users have many pins. (例如,在本年度我最喜欢的专辑中排名第一)这些图钉属于用户,并且用户有很多图钉。 What I want to do is create a new page for 2013 that displays each user, then lists a descending ordered list of the album image, title, artist as their top ten list for the year. 我想做的是为2013年创建一个新页面,该页面显示每个用户,然后列出专辑图像,标题,艺术家的降序列表,作为该年度的前十名。 Something like: 就像是:

<% @users.each do |user| %>
<%= link_to (image_tag user.image(:small)), user %> <%= user.name %>
<ol>   
  <li><%= @pin.album %> - <% @pin.artist%></li>     
    </ol>   
<% end %>

I need to limit the pins to only :date => "2013" and I need to list them in descending order. 我需要将引脚限制为:date =>“ 2013”​​,并按降序列出。

I'm having trouble figuring out the controller and view. 我在弄清楚控制器和视图时遇到了麻烦。 Here is my page controller so far: 到目前为止,这是我的页面控制器:

def tens2013
  @users = User.all
  @pin = Pin.where(:date => "2013")
end

How should I set my controller to be able to call <%= @pin.user.album %> ? 我应该如何设置控制器以调用<%= @ pin.user.album%>?

  1. To call @pin.user.album , you need to define the dependency first at the model level. 要调用@pin.user.album ,您需要首先在模型级别定义依赖项。 So in the Pin model, you should have belongs_to :users and in the User model, you should include has_many :pins . 因此,在Pin模型中,您应该具有belongs_to :users ,在User模型中,您应该包括has_many :pins Now this will assume that in the Pin model, there is a field called user_id , which will be the foreign key. 现在,假设在Pin模型中,有一个名为user_id的字段,它将作为外键。 Also use :include in the queries when you are going to access dependent classes like this. 当您要访问类似的依赖类时,也:include在查询中使用:include It avoids the N+1 query problem. 它避免了N + 1查询问题。 eg: 例如:

     @pin = Pin.includes(:users).where("date >= ?","2013-01-01") 
  2. To limit responses to only the year 2013, you may want to search your query likewise: 要将响应限制为仅2013年,您可能希望同样搜索查询:

     @pin = Pin.where("date >= ?","2013-01-01") 

The way I see it, your models should be set up like this: 以我的看法,您的模型应按以下方式设置:

class User < ActiveRecord::Base
  has_many :pins
end

class Pin < ActiveRecord::Base
  belongs_to :user
  belongs_to :album
end

class Album < ActiveRecord::Base
  has_many :pins
end

Pin should have user_id and album_id to achieve this. Pin应具有user_idalbum_id以实现此目的。

Then in the controller, you can eager load all users and their pins, with each pin's respective album, like this: 然后,在控制器中,您可以急于加载所有用户及其引脚,以及每个引脚各自的相册,如下所示:

@users = User.includes(:pins => :album)

Or to limit to a certain year, do: 或限制为某年,请执行以下操作:

@users = User.includes(:pins => :album).where('pins.date>=?','2013-01-01').references(:pins)

Now you can iterate through the users in your view, and through each user's pins, and each pin's album. 现在,您可以遍历视图中的用户,每个用户的图钉以及每个图钉的相册。

You don't need to use @pin for each user's pin. 您无需为每个用户的图钉使用@pin Make the necessary changes in your view, and iterate through them using this style: 在视图中进行必要的更改,然后使用以下样式进行遍历:

@users.each do |user|
  # do something 
  user.pins.each do |pin|
    # Now you have "pin" and you can use it:
    # pin.album...
    # pin.artist...
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM