Trying to print all instances of the company and product in a join table
but unable to get the name : actually just trying to print every
instance of a company in the join table
allocation.rb
class Allocation < ActiveRecord::Base
belongs_to :company
belongs_to :product
end
company.rb
class Company < ActiveRecord::Base
has_many :products
has_many :allocations
has_many :products, :through => :allocations
end
product.rb
class Product < ActiveRecord::Base
belongs_to :company
has_many :allocations
has_many :companies, :through => :allocations
end
Controller:
class AllocationsController < ApplicationController
def show
@company = Company.find(params[:id])
@allocations = @company.allocations.all
end
def index
@allocations = Allocation.all
end
def new
@allocations = Allocation.new
end
def create
@allocations = Allocations.new(params[:allocations])
if @allocations.save
flash[:notice] = “Successfully created allocations.”
redirect_to @allocations
else
render :action => ‘new’
end
end
The index html page
<% title “Allocations” %>
| Company | Product | Quantity |
|---|---|---|
| <%=h alloc.company_id %> | <%=h alloc.product_id %> | <%=h alloc.quantity %> |
Is there a way to loop through each row, print the company name and the
product in the join table?
I have tried this but get error: no method allocations defined
<% title “Allocations” %>
<% @company.allocations.each_with_index do |allocation, index| %>
<%= allocation.product.name %>
…
<% end %>