Sebastian Plasschaert
Sebastian Plasschaert

Reputation: 304

How to show field from a table that connects another table?

I have a Project model that can contain many Products through the model Benefit (and vice versa for the Product model). Within the Benefits tabel a project_id and product_id are uniquely indexed.

I've created the Benefit model to be able to add a product to multiple projects and I want to add extra data through every benefit (benefit.title etc.).

I'm able to show the products per project, but I'm not able to show any other benefit.value.

My Project model is connected to Benefits like this:

has_many :benefits
has_many :products, through: :benefits

In my Project view (show.html.erb) I'm showing my product field like this:

<% @products.each do |product| %>
                    <%= link_to [product.project, product] do %>    
                        <div class="row">
                                <div class="col m3">
                                    <img height="20" src="<%= product.image %>"></img>
                                </div>
                                <div class="col m3">
                                    <%= product.name %>
                                </div>
                                <div class="col m3 truncate">
                                    <%= product.content %>
                                </div>                                  
                                <div class="col m3">
                                    <i class="tiny red-text material-icons right">favorite_border</i>
                                </div>
                        </div>
                    <% end %>
                <% end %>

But it tells me that I'm using an undefined local variable or method 'Benefit', when I try to add the following:

<% @products.each do |product| %>
                    <%= link_to [product.project, product] do %>    
                        <div class="row">
                                <div class="col m3">
                                    <img height="20" src="<%= product.image %>"></img>
                                </div>
                                <div class="col m3">
                                    <%= product.name %>
                                </div>
                                <div class="col m3 truncate">
                                    <%= benefit.title %>
                                </div>                                  
                                <div class="col m3">
                                    <i class="tiny red-text material-icons right">favorite_border</i>
                                </div>
                        </div>
                    <% end %>
                <% end %>

Any ideas on how I should handle this?

I'm using Ruby-on-Rails 4.2.0 and I'm a novice (as you can clearly see ;) )

Extra code of my product model:

class Product < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: [:slugged, :finders]

belongs_to :project

belongs_to :benefit

has_many :subscriptions
has_many :users, through: :subscriptions

has_many :benefits
has_many :projects, through: :benefits

has_many :reviews

validates :name, presence: true, length: { maximum: 50 }
validates :gtin, presence: true
validates :content, presence: true, length: { maximum: 500 }
validates :video, presence: true
validates :tag, presence: false
validates :project, presence: true

has_attached_file :image, :styles => { :medium => "680x300>", :thumb => "170x75>" }
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/

def average_rating
    reviews.blank? ? 0 : reviews.average(:star).round(2)
end
end

*Update Working code: Based on the comments in this question I was able to show a list of products (that are connected to the project) with any benefit-field connected to it. I created an extra loop by mistake, that's solved now. Here's my final working code in my project/show.html.erb:

<% @products.each do |product| %>
                <% product.benefits.each do |benefit| %>
                    <%= link_to [product.project, product] do %>    
                        <div class="row">
                                <div class="col m3">
                                    <img height="20" src="<%= product.image %>"></img>
                                </div>
                                <div class="col m3">
                                    <%= product.name %>
                                </div>
                                <div class="col m3 truncate">
                                    <%= product.benefits.first.title %>
                                </div>                                  
                                <div class="col m3">
                                    <i class="tiny red-text material-icons right">favorite_border</i>
                                </div>
                        </div>
                    <% end %>
                <% end %>
            <% end %>

Upvotes: 0

Views: 80

Answers (1)

akbarbin
akbarbin

Reputation: 5105

UPDATE: In this issue, you have to loop each benefits form product because you can't get has many associations at one.

You have to use object product to call benefit. Try this to loop each benefit:

<%= product.benefits.each | benefit| %>
  <div class="col m3 truncate">
    <%= benefit.title %>
  </div>
<% end %>

Try this to get first product benefit:

<%= product.benefits.first.title %>

This is to get all benefit join (,):

<%= product.benefits.map(&:title).join(", ") %>

Upvotes: 2

Related Questions