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.
精彩评论