Created
January 16, 2015 19:50
-
-
Save aristotelesbr/ccaa9490c079af9437c3 to your computer and use it in GitHub Desktop.
O relacionamento Associação has_many :through esta correto nesta situação?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
has_many :sales | |
has_many :products, :through => :sales | |
end | |
class Sale < ActiveRecord::Base | |
belongs_to :product | |
belongs_to :user | |
end | |
class Product < ActiveRecord::Base | |
has_many :sales | |
has_many :users, :through => :sales | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% @sales.each do |sale| %> | |
<tr> | |
<td><%= sale.product.name %></td> | |
<td><%= sale.quantity %></td> | |
<td><%= sale.user.email %></td> | |
<td><%= link_to 'Show', sale %></td> | |
<td><%= link_to 'Edit', edit_sale_path(sale) %></td> | |
<td><%= link_to 'Destroy', sale, method: :delete, data: { confirm: 'Are you sure?' } %></td> | |
</tr> | |
<% end %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment