Skip to content

Commit

Permalink
has_many :through (p108-110)
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanbibat committed Sep 4, 2011
1 parent 3ddd554 commit f528031
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/models/product.rb
@@ -1,6 +1,7 @@
class Product < ActiveRecord::Base
validates_presence_of :name, :description
has_many :line_items
has_many :purchases, :through => :line_items

before_validation :assign_default_description

Expand Down
1 change: 1 addition & 0 deletions app/models/purchase.rb
Expand Up @@ -2,6 +2,7 @@ class Purchase < ActiveRecord::Base
has_one :invoice
belongs_to :supplier
has_many :line_items
has_many :products, :through => :line_items

def display_supplier
return supplier.name unless supplier.nil?
Expand Down
7 changes: 7 additions & 0 deletions app/views/products/show.html.erb
Expand Up @@ -22,6 +22,13 @@
<%= @product.stock %>
</p>

<h2>Related Purchases</h2>
<ul>
<% @product.purchases.each do |purchase| %>
<li><%= link_to purchase.description, purchase %></li>
<% end %>
</ul>

<%= link_to 'Edit this record', edit_product_path(@product) %> <br />
<%= link_to 'Delete this record', @product,
{ :confirm => 'Are you sure?', :method => :delete } %> <br />
Expand Down
7 changes: 7 additions & 0 deletions app/views/purchases/show.html.erb
Expand Up @@ -41,5 +41,12 @@

<p><%= link_to "New Line Item", new_purchase_line_item_path(@purchase) %></p>

<h2>Related Products</h2>
<ul>
<% @purchase.products.each do |product| %>
<li><%= link_to product.name, product %></li>
<% end %>
</ul>

<%= link_to 'Edit', edit_purchase_path(@purchase) %> |
<%= link_to 'Back', purchases_path %>

0 comments on commit f528031

Please sign in to comment.