My Rails应用程序具有用户模型和引脚模型。引脚是发布到网站的专辑评论,并且引脚属于用户。我在网站上有一个统计信息页面,其中列出了有关该网站内容的有趣统计信息。我正在尝试列出一个“本周评论者”,它将显示哪个用户在过去一周发布了最多的评论。这是我放在页面控制器中的代码:
@user_week = User.includes(:pins).where('created_at >= ?', 1.week.ago.utc).count(:all, :group => 'name', :order => 'count(*) DESC').first
然后我在视图中调用它:
<%= @user_week[0] %>
出于某种原因,这给了我一个用户,但实际上并不是上周发布过多帖子的用户。它给了我[“用户”,1]并且当我更频繁地发布时它不会改变。我的代码错了吗?
另外,如何从@user_week调用用户的图像?它目前返回名称和引脚数。
用户列名:
["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"]
Pin列名称:
["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"]
答案 0 :(得分:1)
@users = User.joins(:pins).group('users.id').where('pins.created_at >= ?', 1.week.ago.utc).order('count(pins.id) desc').limit(1).first
将返回一组用户。然后使用以下代码
<%= link_to (image_tag @user.image(:small)), @user %>
答案 1 :(得分:0)
我能够在视图中使用它来显示user.image:
<% @users.each do |user| %>
<% if @user_week[0] == user.name %>
<%= link_to (image_tag user.image(:small)), user %>
<% end %>
<% end %>