开发者

Rails help rewriting random mysql finder to PostgreSQL

开发者 https://www.devze.com 2023-02-18 04:11 出处:网络
My controller: def toppen @top = Konkurrancer end My view: <ul id=\"random\"> <% @top.find(:all, :limit => 5, :order => \'rand()\').each do |vind| %>

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

精彩评论

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