19
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

railsでランダムに複数のレコードを取得する

Last updated at Posted at 2016-08-24

railsでランダムに複数のレコードを取得するときのアレ。

例えば4つ取ってくるとき

hoges_controller.rb
@hoges = Hoge.order("RAND()").limit(4)
views/hoges/index.html.erb
<% @hoges.each do |hoge| %>
    任意のコード
<% end %>

いくつかググってみたんですけど

コントローラーの方のRAND()の部分が

RANDOM()

↑このように書いてあるやつもあったんですが

mysqlではRAND()にしないとダメっぽいです。

19
17
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?