2

I get an error while using find_all_by_

controller
@books = Book.find_by_author_id(4)

View
<%= @books.name %>

This works. But when I replace find_by_ with find_all_by_ I get this error

undefined method `name'

I want to use find_all_by_ to fetch all the books corresponding to author_id=4

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Isabella
  • 37
  • 4

1 Answers1

7
Controller
@books = Book.find_all_by_author_id(4)

View
<%= @books.map(&:name).join(', ') %>

you are getting an Array of records. And Array does not have the name method.

zed_0xff
  • 32,417
  • 7
  • 53
  • 72