简体   繁体   中英

Random Generation of Items from Existing Model

I am fairly new to RoR and trying to get a basic app to work - I have a 'books' model and a 'genre' model. I wish to create a page that randomly generates books of different genre's for a user to select.

I have created a 'random_book' controller, but am unsure on how to proceed with the random selection and display.

Any help/pointers would be appreciated.

Edit:

Here's the work I've been doing in the random_book model:

" load 'user.rb'

class random_book < ActiveRecord::Base

 belongs_to :book
 belongs_to :genre


def get_random_book
    find(:all).sample(5)
end

"

Thank you.

Based on discussion

4 models

  1. Book
  2. Genre
  3. UserBook
  4. User

They will look something like this:

class User < ActiveRecord::Base
  has_many :user_books
  has_many :books, through: :user_books
end

class Genre < ActiveRecord::Base
  has_many :books

  def fetch_random_books(qty)
    #you want to make sure you don't error out by requesting a sample of an empty list of books so check first.  The qty argument lets you control the number of books for the search
    unless self.books.empty?
      self.books.limit(qty).sample
    end
  end
end

class Book < ActiveRecord::Base
  belongs_to :genre
  has_many :user_books
end

class UserBook < ActiveRecord::Base
  belongs_to :book
end

I would most likely use a different route for the random book section, because it's not very url-friendly to say code-descriptive things like user_book.

There are 4 things to do

  1. Create a new route to get a list of genre_ids that a user chooses.
  2. Create an action that correlates to the route you created that renders a list of boos and adds those books to a users list
  3. Create a form in a view (any view, like a sidebar in an existing books view, doesn't matter) this form will post the route and action you just made
  4. Create a view to render the book list (the easy / DRY way is to add a few elements to the existing books index to let users know its a random generated list of books based on their genre pics)

Add the route

post "/random-books", to: "books#random_books", as: :random_books

Create the action in the books_controler

def random_books
  if params[:genre_ids]
    genres = Genre.where(id: params[:genre_ids])
    @books = []
    genres.each do |genre|
      @books << genre.fetch_random_books(10)
    end

  else
    @books = nil
  end
  respond_to do |format|
    format.html { render action: :index }

  end
end

Then create a form that makes a post request to the index action of the books_controller -- You can parse the form and update the UserBook model inside that action, and then display list of books all at the same time.

<%= form_tag(random_books_path, method: :post) do %>
  <ul>
    <% Genre.all.each do |genre| %>
      <li>
      <label class='genre-select'>
        <%= check_box_tag 'genre_ids[]', genre.id -%>
        <%=  genre.name %>
      </label>

      </li>
    <% end %>
  </ul>
  <%= submit_tag "Fetch Book List"%>
<% end %>

-- The last one I'm sure you can do, it returns a books object list so parse it however works best for you. In the controller action you can automatically add the ids for the books to the UserBook model by adding this inside the controller action:

      @books.each{ |book| book.user_books.create(user: user)}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM