Skip to content

Commit

Permalink
elation between products and categories
Browse files Browse the repository at this point in the history
  • Loading branch information
mariochavez committed Jan 26, 2013
1 parent ec5a033 commit 8c5ab31
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 1 deletion.
4 changes: 4 additions & 0 deletions app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ def create
end
render :new
end

def show
@product = Product.find_by_id params[:id]
end
end
7 changes: 7 additions & 0 deletions app/models/category.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Category < ActiveRecord::Base
has_many :products

attr_accessible :name

validates :name, presence: true
end
2 changes: 2 additions & 0 deletions app/models/product.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class Product < ActiveRecord::Base
before_save :set_defaults

belongs_to :category

attr_accessible :active, :description, :name, :price

validates :name, presence: true
Expand Down
13 changes: 13 additions & 0 deletions app/views/products/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<h1>Product</h1>
<% if @product.nil? %>
<h3>Product not found</h3>
<% else %>
<p>
<span>Name: </span><%= @product.name %>
</p>
<p>
<span>Description: </span><%= @product.description %>
</p>
<p>
<% end %>
<%= link_to :back %>
9 changes: 9 additions & 0 deletions db/migrate/20130126203942_create_categories.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateCategories < ActiveRecord::Migration
def change
create_table :categories do |t|
t.string :name

t.timestamps
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20130126204442_add_category_id_to_product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddCategoryIdToProduct < ActiveRecord::Migration
def change
add_column :products, :category_id, :integer
end
end
9 changes: 8 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20130126162834) do
ActiveRecord::Schema.define(:version => 20130126204442) do

create_table "categories", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

create_table "products", :force => true do |t|
t.string "name"
Expand All @@ -20,6 +26,7 @@
t.boolean "active"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "category_id"
end

end
7 changes: 7 additions & 0 deletions test/fixtures/categories.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html

one:
name: MyString

two:
name: MyString
7 changes: 7 additions & 0 deletions test/unit/category_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class CategoryTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 8c5ab31

Please sign in to comment.