Join works in the rails console, but not the view

Viewed 49

I'm working in Rails 5. I have two tables with a HABTM relationship: publications and authors. They're connected by a join table, authors_publications.

I'm trying to get the view to list the names of the authors associated with each publication, and it's returning this: the view

It's got the number of authors correct, but I want it to print the actual names. When I try this in my console, the join works correctly. Here's the relevant code from my app:

publication.rb

class Publication < ApplicationRecord

  self.table_name = "publications"

  has_and_belongs_to_many :authors

  scope :sorted, lambda { order("year_published DESC") }
  scope :newest_first, lambda { order("created_at DESC") }
  #scope :search, lambda {|query| where(["name LIKE ?", "%#{{query}}"])
#}

end

author.rb

class Author < ApplicationRecord

  self.table_name = "authors"

  has_and_belongs_to_many :publications

end

index.html.erb

<% @publications.each do |publication| %>
    <h3><%= publication.name %></h3>
    <p><%= publication.citation %></p>
    <p><%= publication.authors.join(" ") %></p>                
<% end %>

How do I make this print the author names associated with each publication instead of printing <Author:xxxxxxxxxxxxxxxx>#?

1 Answers
Related