Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ==== Generate ===========
- rails g model ProductRatings product:references user:references rating:integer comment:text
- rails g model MarketRatings market:references user:references rating:integer comment:text
- rails g model ProductsMarkets product_id:references market_id:references
- rails g model market name:string address:string description:text facebook_event_id:string category:integer users:references
- rails g model Products users:references name:string price:decimal picture:string category:integer
- rails g model Users first_name:string last_name:string password:string email:string phone_number:string role:integer
- ==== Create ===========
- User.create!(first_name: "Bob", last_name: "Enek", password: "siema", email: "bobenek@gmail.com")
- Product.create!(name: "Pumpkin", price: 21.37, category: 1, user_id: 1)
- Market.create!(name: "Pumpkin festival", address: "Lodz, Piotrkowska 12", description: "yes yes yes yes yes", category: 1, user_id: 1)
- ProductsMarket.create(product_id: 1, market_id: 1)
- ProductRating.create!(user_id: 1, product_id: 1, rating: 2, comment: "siema elo")
- MarketRating.create!(user_id: 1, market_id: 1, rating: 2, comment: "siema elo")
- ==== Class ===========
- class MarketRating < ApplicationRecord
- belongs_to :market
- belongs_to :user
- end
- class Product < ApplicationRecord
- belongs_to :user
- has_many :products_markets
- has_many :markets, through: :products_markets
- has_many :product_ratings
- end
- class ProductsMarket < ApplicationRecord
- belongs_to :product
- belongs_to :market
- end
- class ProductRating < ApplicationRecord
- belongs_to :product
- belongs_to :user
- end
- class User < ApplicationRecord
- has_many :products
- has_many :markets
- has_many :market_ratings
- has_many :product_ratings
- end
- class Market < ApplicationRecord
- belongs_to :user
- has_many :products_markets
- has_many :products, through: :products_markets
- has_many :market_ratings
- has_many :product_ratings
- end
- ==== Migrations ===========
- class CreateUsers < ActiveRecord::Migration[5.2]
- def change
- create_table :users do |t|
- t.string :first_name, null: false
- t.string :last_name, null: false
- t.string :password, null: false
- t.string :email, null: false
- t.string :phone_number
- t.integer :role, null: false, default: 0
- t.timestamps
- end
- end
- end
- class CreateProducts < ActiveRecord::Migration[5.2]
- def change
- create_table :products do |t|
- t.references :user, foreign_key: true
- t.string :name, null: false
- t.decimal :price, null: false
- t.string :picture
- t.integer :category, null: false
- t.timestamps
- end
- end
- end
- class CreateMarkets < ActiveRecord::Migration[5.2]
- def change
- create_table :markets do |t|
- t.string :name, null: false
- t.string :address, null: false
- t.text :description
- t.string :facebook_event_id
- t.integer :category, null: false
- t.references :user, foreign_key: true
- t.timestamps
- end
- end
- end
- class CreateProductsMarkets < ActiveRecord::Migration[5.2]
- def change
- create_table :products_markets do |t|
- t.references :product, foreign_key: true
- t.references :market, foreign_key: true
- t.timestamps
- end
- end
- end
- class CreateMarketRatings < ActiveRecord::Migration[5.2]
- def change
- create_table :market_ratings do |t|
- t.references :market, foreign_key: true
- t.references :user, foreign_key: true
- t.integer :rating
- t.text :comment
- t.timestamps
- end
- end
- end
- class CreateProductRatings < ActiveRecord::Migration[5.2]
- def change
- create_table :product_ratings do |t|
- t.references :product, foreign_key: true
- t.references :user, foreign_key: true
- t.integer :rating
- t.text :comment
- t.timestamps
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement