[Rails] I could use .sample when I wanted one record at random from the table.

Challenges and conclusions

If I wanted a random record from the table, I could use .sample normally.

python


irb(main):005:0> User.all.sample(1)
=> [#<User id: 5, name: 'aho'>]

The basics I didn't know soberly.

Reference information

What exactly is ActiveRecord :: Relation? https://doruby.jp/users/whale/entries/ActiveRecord--Relation%E3%81%A8%E3%81%AF%E4%B8%80%E4%BD%93%E3%81%AA%E3%82%93%E3%81%AA%E3%81%AE%E3%81%8B

Recommended Posts

[Rails] I could use .sample when I wanted one record at random from the table.
[Rails] When I use form_with, the screen freezes! ??
[Rails] I tried to raise the Rails version from 5.0 to 5.2
[Rails] I don't know how to use the model ...
What should I use for the testing framework [Rails]