Rails Choose 3 different posts randomly

Related searches

hey everyone so I'm working on a small blog and there is a new section a want to add. An "editor's choice" section so basically i want to add a small div that contains 3 randomly pick posts from the blog. The problem is I do not know how to show only three randomely select titles from all the posts. I tried @posts.sample(3) and that shows the entire record of the Post. I just want to show the title only. I want to show the editors choice on the posts index view Schema

  create_table "posts", force: :cascade do |t|
    t.string "title"
    t.text "content"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.integer "user_id"
    t.string "nickname"
  end

Posts_controller

class PostsController < ApplicationController
  before_action :current, only: [:show, :edit, :update, :destroy]
  def index
    @posts = Post.all
  end

  def show
  end

  def new
    @post = Post.new
  end

  def create
    @post = Post.new(posts_params)
    @post.user = current_user
    if @post.save
      redirect_to posts_path
    else
      render :new
    end
  end

  def edit
  end

  def update
    @post.user = current_user
    @post.update(posts_params)
    @post.save
    redirect_to posts_path
  end

  def destroy
    if @post.destroy
      redirect_to posts_path
    end
  end

  private

  def posts_params
    params.require(:post).permit(:title, :content, :user_id, :image, :nickname)
  end

  def current
    @post = Post.find_by(id: params[:id])
  end

end

post_index.html.erb

<%= render "layouts/blog_header" %>
<% @posts.each do |post| %>
  <div class="posts">
    <div>
      <% unless !post.image.attached? %>
        <%= image_tag post.image, class: "post-index-image" %><br>
      <% end %>
    </div>
    <p class="post-index-title"><%= link_to post.title, post_path(post.id), class: "post-index-title" %></p>
    <p>Published by: <strong><%= post.user.nickname %></strong></p>
  </div>
<% end %>
<%= @posts.sample(3) %>

Thanks!

I would start with something like this when you are using PostgreSQL:

@posts.order('RANDOM()').limit(3)

or when you are on MySQL:

@posts.order('RAND()').limit(3)

Rails Quick Tips, Heading image for post: Rails Quick Tips - Random Records This solves the problem but why would we do something different? First we are returning Then we shuffle that array and choose the first 5 records. Well this is� Using a sledgehammer, drive the posts into the holes. For added stability, you can fill the remaining space around each post with cement. Trim the tops of the posts to the desired height of the fence. Mark the posts for bottom rails that are 3 to 4 inches above the ground and top rails flush with the top of the posts.

I think it should do what you want:

@posts.sample(3).pluck(:title)

Easily select random records in rails, If you ever wondered how to easily retrieve a random record in an end EDIT ( 30/11/2012): after revisiting this post I obviously found out that this is in Other solutions try to get pseudo random records using the id column,� Depending on the size of the deck you may only need a railing post at each corner and on either side of the stairs (top and bottom). The maximum distance between posts will be the length of your top rail. The top rail will need to fasten securely to a post at each end.

I'd go with

@posts.pluck(:title).sample(3)

More efficient than

@posts.sample(3).pluck(:title)

How to Generate Random Numbers & Strings in Ruby, With a random number you can pick a random element from an array, pick a winner In Ruby, there are many ways to generate random numbers with various properties. The rand method can be used in 3 ways: Found this post useful? Posts on the inside. With posts on the inside of deck joists joist, you can use two corner posts, as shown in the detail above. With two corner posts, the top rails extend beyond the posts and meet at the corner. Another option is to use a single post at the corner, installed the same way as the inside posts in Detail A, above.

Securing Rails Applications — Ruby on Rails Guides, Securing Rails ApplicationsThis manual describes common security Including a nonce (a random value) in the session solves replay attacks. forces the user's browser into using this session ID (see number 3 in the image). The World Wide Web Consortium (W3C) provides a checklist for choosing HTTP GET or POST:. 2b. Post Sleeve Skirt Actual external dimensions. 4” x 4”: 4.55” x 4.55” (115 mm x 115 mm) 6” x 6”: 5.55” x 5.55” (140 mm x 140 mm) 12 per box. Finishing piece at the base of the post that covers any cuts where the decking and railing meet. Usually coordinates with the cap. 6” Available in Classic White only.

Ruby On Rails Security Guide, This manual describes common security problems in web applications and how to of the Ruby interpreter (also basically a random number) and a constant string. the user's browser into using this session id (see number 3 in the image) . Web Consortium (W3C) provides a checklist for choosing HTTP GET or POST: . Also, there are other ways to randomly select an item from a list lets see those now. Randomly select multiple choices from the list. As you know, the random.choice function only returns a single item from a list. If you want to randomly select more than one item from a list or set, I’d recommend using random.sample() or random.choice s instead.

There are three very-lightly-documented hash accessors on the request object for this: request.query_parameters - sent as part of the query string, i.e. after a ?; request.path_parameters - decoded from the URL via routing, i.e. controller, action, id

Comments
  • @posts.sample.pluck(:title) - should work :)