简体   繁体   English

如何使用Ruby on Rails框架通过评分显示照片?

[英]How to display photos by rating with Ruby on Rails framework?

Ok. 好。 I've got a model Picture 我有模特照片

class Picture < ActiveRecord::Base
  has_one  :rating
  has_many :comments
end

And, of course Rating model 而且,当然是评分模型

class Rating < ActiveRecord::Base
  belongs_to :picture
end

I made rating system by myself. 我自己制定了评分系统。 So, now on the frontpage I need to display all photos by rating. 因此,现在在首页上,我需要按等级显示所有照片。 Please, can u show me how to do that. 拜托,你能告诉我怎么做吗。 I've no idea what will be in index.html.erb of main controller. 我不知道主控制器的index.html.erb中会是什么。

If Rating has an integer field "value", you can do this: 如果Rating具有整数字段“ value”,则可以执行以下操作:

class Picture < ActiveRecord::Base
  has_one  :rating

  named_scope :by_rating, { :joins => :rating, :order => 'ratings.value DESC' }
end

Now, in the controller you can use this: 现在,在控制器中,您可以使用以下命令:

@pictures_top10 = Picture.by_rating.all :limit => 10

In the view: 在视图中:

<% @pictures_top10.each do |picture| %>
  <%= image_tag(picture.url) %>
<% end %>

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

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