Ruby on rails Rails-查找上周帖子最多的用户
我的Rails应用程序有一个用户模型和一个PIN模型。PIN是发布到站点的相册评论,PIN属于用户。我在网站上有一个统计页面,列出了关于网站内容的有趣统计信息。我正在尝试列出一个“本周评论员”,它将显示哪位用户在上周发表的评论最多。这是我放在页面控制器中的代码:Ruby on rails Rails-查找上周帖子最多的用户,ruby-on-rails,ruby-on-rails-3,activerecord,Ruby On Rails,Ruby On Rails 3,Activerecord,我的Rails应用程序有一个用户模型和一个PIN模型。PIN是发布到站点的相册评论,PIN属于用户。我在网站上有一个统计页面,列出了关于网站内容的有趣统计信息。我正在尝试列出一个“本周评论员”,它将显示哪位用户在上周发表的评论最多。这是我放在页面控制器中的代码: @user_week = User.includes(:pins).where('created_at >= ?', 1.week.ago.utc).count(:all, :group => 'name', :order
@user_week = User.includes(:pins).where('created_at >= ?', 1.week.ago.utc).count(:all, :group => 'name', :order => 'count(*) DESC').first
然后我在视图中称之为:
<%= @user_week[0] %>
<% @users.each do |user| %>
<% if @user_week[0] == user.name %>
<%= link_to (image_tag user.image(:small)), user %>
<% end %>
<% end %>
Pin列名称:
["id", "email", "encrypted_password", "reset_password_token", "reset_password_sent_at", "remember_created_at", "sign_in_count", "current_sign_in_at", "last_sign_in_at", "current_sign_in_ip", "last_sign_in_ip", "created_at", "updated_at", "name", "image_file_name", "image_content_type", "image_file_size", "image_updated_at", "slug", "twitter", "lastfm", "status", "admin"]
["id", "description", "created_at", "updated_at", "user_id", "image_file_name", "image_content_type", "image_file_size", "image_updated_at", "image_remote_url", "artist", "album", "date", "rank", "video_html", "video", "rating", "year"]
通过在视图中使用此选项,我可以获得要显示的user.image:
<%= @user_week[0] %>
<% @users.each do |user| %>
<% if @user_week[0] == user.name %>
<%= link_to (image_tag user.image(:small)), user %>
<% end %>
<% end %>
将返回一个用户数组。然后使用以下代码
<%= link_to (image_tag @user.image(:small)), @user %>
你能发布user和pins模型的列吗?@arun15th我可以在上面添加它们@user\u week=user.joins(:pins)。其中('pins.created_at>=?',1.week.ago.utc)。count(:all,:group=>'name',:order=>'count(*)DESC')
你能试试这个吗?@arun15th有效!你就是那个人。现在您知道如何在视图中显示用户_week.image了吗。user_week现在返回一个数组,如[“Tony”,4]是返回这样的数组还是只返回一个这样的数组?您在@arun15thmay上说得很有道理,但是上面的代码返回了一个数组,user.image不能与数组一起使用。也许我可以限制Desive不允许两个用户使用相同的用户名?@TonyTambe如果是用户名,你可以尝试限制两个用户使用相同的用户名。如果是用户的名字,我不认为限制是个好主意。在任何情况下,如果您使用
而不是
,我相信我的代码的第二部分应该是有效的,但这将列出每个用户的图像。我只需要列出我通过user_week获得的用户,它返回[“用户名”,评论数]