DetectiveClarke DetectiveClarke - 7 months ago 15
Ruby Question

Ruby on Rails : Storing the pluck result

Im creating a library management system as part of a college assignment.
Im currently stuck on obtaining the result of pluck.

The task Im trying to do here is to display a list of books that are currently in the table.
Ive been primarily a PHP programmer so ruby on rails is a whole new world for me.

From doing external research I found out about object.pluck.

Book.pluck(:isbn,:title,:description,:author,:status)


My knowledge of the above snippet is that Im pulling from all book objects there details.
What I want to do is have a loop that will output each book followed by an edit button.
In PHP this would be a form inside of a for loop. but with ruby I dont really know how to do it

In my head I want to store the result of the pluck in an array of books[].
Then use a loop to iterate through that loop and output details of the book stored at index i.
But Im not sure how to store the result of pluck?

Answer

You can assign the whole books collection to a variable, and then use it in view:

# in controller's action (most likely index)

@books = Book.all

# in index.html.* view (haml example):

- @books.each do |book|
  = link_to 'Edit book', edit_book_path(book)

Make sure you go through the docs (really useful).