grocRB

$ cd grocRB
# Update Gemfile:    set sqlite3 for dev/test and pg for production. Use debugger gem. Use haml
$ bundle install
set static pages
$ vim app/controllers/pages_controller.rb
class PagesController < ApplicationController
  def home
  end

  def about
  end

  def contact
  end
end
$ vim app/views/pages/home.html.haml
%h1 hi!
$ vim config/routes.rb
root 'pages#home'                         OR root :to => redirect('/movies')
match '/home', to: 'pages#home', via: :GET
match '/about', to: 'pages#about', via: :GET
match '/contact', to: 'pages#contact', via: :GET
$ git init .
$ git remote add origin git@github.com:mshahriarinia/grocRB.git
$ git add -A
$ git commit -m "Initial commit"
$ git push
$ heroku create
$ git remote add heroku git@heroku.com:pure-tundra-4301.git                                         # gem install heroku
$ git push heroku master

Add stuff for product type
$ vim config/routes.rb
resources :product_types
$ rake routes

$ vim app/controllers/product_types_controller.rb
 class ProductTypesController < ApplicationController

  def show
  end

  def index
  end

  def new
  end

  def create
  end

  def edit
  end

  def update
  end
 
  def destroy
  end

end

$ rails generate migration product_types
   create_table :product_types do |t|
      t.string :name
      t.string :brand
      t.text :description
 
      t.timestamps
   end
$ rake db:migrate
$ vim app/models/product_type.rb
class ProductType < ActiveRecord::Base
end
Follow the rest from here:

for css file to take effect convert application.html.erb to application.html.haml
!!! 5
%html
  %head
    %title Rotten Potatoes!
    = stylesheet_link_tag 'application'
    = javascript_include_tag 'application'
    = csrf_meta_tags

  %body
    %h1.title Rotten Potatoes!
    #main
      - if flash[:notice]
        #notice.message= flash[:notice]
      - elsif flash[:warning]
        #warning.message= flash[:warning]

      = yield
put all your style sheets in app/assets/stylesheets/application.css.scss
html, body {
  margin: 0;
  padding: 0;
  background: White;
  color: DarkSlateGrey;
  font-family: Tahoma, Verdana, sans-serif;
  font-size: 10pt;
}
div#main {
  margin: 0;
  padding: 0 20px 20px;
}
a {
  background: transparent;
  color: maroon;
  text-decoration: underline;
  font-weight: bold;
}
h1 {
  color: maroon;
  font-size: 150%;
  font-style: italic;
  display: block;
  width: 100%;
  border-bottom: 1px solid DarkSlateGrey;
}
h1.title {
  margin: 0 0 1em;
  padding: 10px;
  background-color: orange;
  color: white;
  border-bottom: 4px solid gold;
  font-size: 2em;
  font-style: normal;
}
table#movies {
  margin: 10px;
  border-collapse: collapse;
  width: 100%;
  border-bottom: 2px solid black;
}
table#movies th {
  border: 2px solid white;
  font-weight: bold;
  background-color: wheat;
}
table#movies th, table#movies td {
  padding: 4px;
  text-align: left;
}
#notice #warning {
  background: rosybrown;
  margin: 1em 0;
  padding: 4px;
}
form label {
  display: block;
  line-height: 25px;
  font-weight: bold;
  color: maroon;
}
table#movies th.hilite {
  background-color: yellow;
}

 bundle exec rake tmp:clear

 bundle exec rake assets:clean RAILS_ENV=production

 bundle exec rake assets:precompile RAILS_ENV=production     #for heroku to show css
heroku not loading css?:   vim environments/production.rb
config.assets.compile = true
config.assets.digest = true


==========================
make sure to edit db/seeds.rb and $ rake db:seed


let product desciptions to be markdown.
maybe remove brand??

rake db:migrate:reset    that just runs rake db:drop db:create db:migrate.
If you're wanting to seed a fresh database, you can just run rake db:reset db:seed


bundle exec rake assets:precompile

-----------


for foreign key make sure to add _id eveywhere and belongs to in model


http://stackoverflow.com/questions/14034844/create-association-between-two-instancied-objects

I have two models: (Albums and Product)

1) Inside Models

Inside album.rb:

class Album < ActiveRecord::Base
  attr_accessible :name
  has_many :products
end

Inside product.rb:

class Product < ActiveRecord::Base
  attr_accessible :img, :name, :price, :quantity
  belongs_to :album
end

2) Using "rails console", how can I set the associations (so I can use "<%= Product.first.album.name %>")?

e.g.

a = Album.create( :name => "My Album" )
p = Product.create( :name => "Shampoo X" )
# what's next? how can i set the album and the product together?
asked Dec 26 '12 at 0:01

add comment

2 Answers

up vote 5 down vote accepted

You can do like this:

a = Album.create( name: "My Album" )

p = Product.create( name: "Shampoo X" )
# OR
p = Product.create( name: "Shampoo X", album_id: a.id )
# OR
p.album = a
# OR
p.album_id = a.id
# OR 
a.products << a
# finish with a save of the object:
p.save

You may have to set the attribute accessible to album_id on the Product model (not sure about that).

Check @bdares' comment also.

 Bargain.create!(:price => 64, :store_id => 1, :user_id => 1, :product_type_id => 4, :description => 'good deal')









Subpages (1): Devise Authorization
Comments