Skip to content

Instantly share code, notes, and snippets.

@aristotelesbr
Created January 16, 2015 19:50
Show Gist options
  • Save aristotelesbr/ccaa9490c079af9437c3 to your computer and use it in GitHub Desktop.
Save aristotelesbr/ccaa9490c079af9437c3 to your computer and use it in GitHub Desktop.
O relacionamento Associação has_many :through esta correto nesta situação?
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
<% @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