Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. ==== Generate ===========
  2.  
  3. rails g model ProductRatings product:references user:references rating:integer comment:text
  4. rails g model MarketRatings market:references user:references rating:integer comment:text
  5. rails g model ProductsMarkets product_id:references market_id:references
  6. rails g model market name:string address:string description:text facebook_event_id:string category:integer users:references
  7. rails g model Products users:references name:string price:decimal picture:string category:integer
  8. rails g model Users first_name:string last_name:string password:string email:string phone_number:string role:integer
  9.  
  10. ==== Create ===========
  11.  
  12. User.create!(first_name: "Bob", last_name: "Enek", password: "siema", email: "bobenek@gmail.com")
  13. Product.create!(name: "Pumpkin", price: 21.37, category: 1, user_id: 1)
  14. Market.create!(name: "Pumpkin festival", address: "Lodz, Piotrkowska 12", description: "yes yes yes yes yes", category: 1, user_id: 1)
  15. ProductsMarket.create(product_id: 1, market_id: 1)
  16. ProductRating.create!(user_id: 1, product_id: 1, rating: 2, comment: "siema elo")
  17. MarketRating.create!(user_id: 1, market_id: 1, rating: 2, comment: "siema elo")
  18.  
  19. ==== Class ===========
  20.  
  21. class MarketRating < ApplicationRecord
  22. belongs_to :market
  23. belongs_to :user
  24. end
  25.  
  26. class Product < ApplicationRecord
  27. belongs_to :user
  28. has_many :products_markets
  29. has_many :markets, through: :products_markets
  30. has_many :product_ratings
  31. end
  32.  
  33. class ProductsMarket < ApplicationRecord
  34. belongs_to :product
  35. belongs_to :market
  36. end
  37.  
  38. class ProductRating < ApplicationRecord
  39. belongs_to :product
  40. belongs_to :user
  41. end
  42.  
  43. class User < ApplicationRecord
  44. has_many :products
  45. has_many :markets
  46. has_many :market_ratings
  47. has_many :product_ratings
  48. end
  49.  
  50. class Market < ApplicationRecord
  51. belongs_to :user
  52. has_many :products_markets
  53. has_many :products, through: :products_markets
  54. has_many :market_ratings
  55. has_many :product_ratings
  56. end
  57.  
  58. ==== Migrations ===========
  59.  
  60. class CreateUsers < ActiveRecord::Migration[5.2]
  61. def change
  62. create_table :users do |t|
  63. t.string :first_name, null: false
  64. t.string :last_name, null: false
  65. t.string :password, null: false
  66. t.string :email, null: false
  67. t.string :phone_number
  68. t.integer :role, null: false, default: 0
  69.  
  70. t.timestamps
  71. end
  72. end
  73. end
  74.  
  75. class CreateProducts < ActiveRecord::Migration[5.2]
  76. def change
  77. create_table :products do |t|
  78. t.references :user, foreign_key: true
  79. t.string :name, null: false
  80. t.decimal :price, null: false
  81. t.string :picture
  82. t.integer :category, null: false
  83.  
  84. t.timestamps
  85. end
  86. end
  87. end
  88.  
  89. class CreateMarkets < ActiveRecord::Migration[5.2]
  90. def change
  91. create_table :markets do |t|
  92. t.string :name, null: false
  93. t.string :address, null: false
  94. t.text :description
  95. t.string :facebook_event_id
  96. t.integer :category, null: false
  97. t.references :user, foreign_key: true
  98.  
  99. t.timestamps
  100. end
  101. end
  102. end
  103.  
  104. class CreateProductsMarkets < ActiveRecord::Migration[5.2]
  105. def change
  106. create_table :products_markets do |t|
  107. t.references :product, foreign_key: true
  108. t.references :market, foreign_key: true
  109.  
  110. t.timestamps
  111. end
  112. end
  113. end
  114.  
  115. class CreateMarketRatings < ActiveRecord::Migration[5.2]
  116. def change
  117. create_table :market_ratings do |t|
  118. t.references :market, foreign_key: true
  119. t.references :user, foreign_key: true
  120. t.integer :rating
  121. t.text :comment
  122.  
  123. t.timestamps
  124. end
  125. end
  126. end
  127.  
  128. class CreateProductRatings < ActiveRecord::Migration[5.2]
  129. def change
  130. create_table :product_ratings do |t|
  131. t.references :product, foreign_key: true
  132. t.references :user, foreign_key: true
  133. t.integer :rating
  134. t.text :comment
  135.  
  136. t.timestamps
  137. end
  138. end
  139. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement