ruby - rails, best way to get random nested record -


I have a post model and a comment model; There are many comments in the post, and comments belong to a post

In my post controller, I want to select a random comment, and want to display the date of the comment.

In my post controller, I have:

  @posts = Post.where (: public_flag = & gt; true) .dr ('created_at DESC')  

After this I'm stuck a bit. Should I get a comment from this idea? Or are there any other queries from the controller to get the comments? Then, how do I choose a random comment after a comment?

Thanks in advance! Appreciate your help.

Answer to your question Application:

  Offset = rand (@ Post.comments.count) # rails4rand_record = @ post.comments.offset (offset) First # Rail 3 rand_record = @ post.comments.first (: offset = & gt; offset)  

I hope it helps


Comments

Popular posts from this blog

HTML/CSS - Automatically set height width from background image? -

php - Mysql Show Process - Sleep Commands and what to do -

c - What is the address of buf (the local variable in the main function)? -