Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zad6 #270

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open

Zad6 #270

wants to merge 15 commits into from

Conversation

BielinskiLukasz
Copy link

Znów nocka złapała :)

@ahawrylak ahawrylak added the zad6 label Dec 11, 2018
@ahawrylak ahawrylak self-assigned this Dec 11, 2018
Copy link
Collaborator

@ahawrylak ahawrylak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dzięki za rozwiązanie, wygląda bardzo dobrze 👍 Dla przyjemniejszego developmentu można dodać seedy odpalane przez rails db:seed:

# db/seeds.rb

puts 'Creating customers'
customers = []
10.times do
  customers << Customer.create!(name: Faker::LordOfTheRings.character)
end

puts 'Creating categories'
categories = []
5.times do
  categories << Category.create!(name: Faker::GameOfThrones.house)
end

puts 'Creating products'
products = []
20.times do
  products << categories.sample.products.create!(price: rand(1..10_000),
                                                 name: Faker::Pokemon.name)
end

puts 'Ordering products'
products.sample(15).each do |product|
  product.update!(customer: customers.sample)
end

puts 'Done!'

class ProductsController < ApplicationController
def index
@customer = Customer.find(params[:customer_id])
@products = @customer.products.order(price: :desc).includes(:categories)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

order(price: :desc) mógłby stać się scope na modelu Product

class Product < ApplicationRecord
has_many :category_products
has_many :categories, through: :category_products
belongs_to :customer
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

brak opcji optional: true powoduje, że nie można stworzyć produktu, który nie należy jeszcze do żadnego customera 😉

@@ -1,3 +1,5 @@
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
resources :customers do
resources :products
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warto doprecyzować jakie akcje są nam potrzebne: resources :products, only: :index. Bez tego tworzy się mnóstwo niepotrzebnych i niezaimplementowanych ścieżek:
screen shot 2018-12-11 at 16 48 30

@ahawrylak ahawrylak added the ✓ Done in the summary it is considered as done label Dec 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✓ Done in the summary it is considered as done zad6
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants