I am trying to pull out rows from a postgres database, I can pull them out desc but when I try random I get a Syntax error near random.
Error
PG::Error: ERROR: syntax error at or near "rand"
LINE 1: ... "hashtags".* FROM "hashtags" ORDER BY tweet_id rand LIMIT...
^
: SELECT "hashtags".* FROM "hashtags" ORDER BY tweet_id rand LIMIT 4
Code to pull it out
<div id="hashtags">
<% Hashtag.order("tweet_id desc").limit(4).each do |hashtag| %>
<blockquote><%= hashtag.content %></blockquote>
<div class="from">— @<%= hashtag.screen_name %></div>
<% end %>
</div>
random()
; it's usingdesc
. Is that the right code? Also, as an aside, your query should go in your controller, not your view. Finally, I suggest testing stuff like this in the Rails console. – Isaac Cambron Nov 30 '12 at 7:59@random_hashtags = Hashtag.order(...).limit(4)
, and then just refer to it in your view like@random_hashtags.each do...
. – Isaac Cambron Nov 30 '12 at 8:26