thegreengiant
thegreengiant

Reputation: 61

Rails acknowledging association, but not displaying data

bear with me as newbie to RoR, trying to build first app.

I have a basic store that has Customers, Transactions and Sellers, as follows;

class Customer < ActiveRecord::Base

    has_many :trans
    has_many :sellers, through: :trans

end

class Tran < ActiveRecord::Base

    belongs_to :customers
    belongs_to :sellers

end
class Seller < ActiveRecord::Base

    has_many :trans
    has_many :customers, through: :trans
end

and for each Customer show view I am wanting to show a list of transactions (the column title in the Tran model is 'sum').

In my customer show view I have;

<% if @supplier.trans.any? %>
<h3>Transactions (<%= @supplier.trans.count %>) </h3>

<%= @supplier.trans.all %>

The first part, showing a count of the number of transactions works a charm and displays correctly, but the second part always seems to give me an error, such as;

#<Tran::ActiveRecord_AssociationRelation:0x007f999099d488>

Tried searching everywhere to replicate this error, but unable to find solution online. 100% sure it's blindingly obvious to someone who knows what they're doing :-)

Any help appreciated!

Upvotes: 1

Views: 36

Answers (1)

Paweł Obrok
Paweł Obrok

Reputation: 23194

The second part is not an error, but the default String representation of @supplier.trans.all. If you want to display something else then you will have to write something like:

<ol>
<% @supplier.trans.each do |t| %>
  <li><%= t.name %></li>
<% end %>
</ol>

Upvotes: 3

Related Questions