开发者

Rails help rewriting random mysql finder to PostgreSQL

My controller:

def toppen
@top = Konkurrancer
end

My view:

<ul id="random">
<% @top.find(:all, :limit => 5, :order => 'rand()').each do |vind| %>
<li><%= link_to vind.name.force_encoding("UTF-8"), konkurrance_path(vind.kategori.cached_slug, vind.cached开发者_如何学JAVA_slug) %></li>
 <% end %>
 </ul>

How do I rewrite this to PostgreSQL?


In MySQL random is used by calling RAND().

In PostreSQL random is used by calling RANDOM().

So it would be:

<% @top.find(:all, :limit => 5, :order => 'random()').each do |vind| %>

On another note, you should never use ActiveRecord in your views. I'd read up on the concept of MVC.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜