-
Notifications
You must be signed in to change notification settings - Fork 58
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
Finish basic task6 #269
base: master
Are you sure you want to change the base?
Finish basic task6 #269
Conversation
There was a problem hiding this 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, wszystko działa okej 👍 Dla przyjemniejszego developmentu można dodać seedy, które odpala się komendą 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))
end
puts 'Ordering products'
products.sample(15).each do |product|
product.update!(customer: customers.sample)
end
puts 'Done!'
|
||
def create | ||
@customer = Customer.create(customer_params) | ||
if @customer.persisted? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Zazwyczaj pisze się:
@customer = Customer.new(customer_params)
if @customer.save
...
class ProductsController < ApplicationController | ||
def index | ||
@customer = Customer.find(params[:customer_id]) | ||
@products = @customer.products.order(price: :desc) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
order(price: :desc)
mogłoby stać się scope na modelu Product
@@ -0,0 +1,7 @@ | |||
class Product < ApplicationRecord | |||
belongs_to :customer |
There was a problem hiding this comment.
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 jest jeszcze przypisany do żadnego customera
|
||
def product_params | ||
params.require(:product).permit(:price, :customer_id) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ta metoda nie jest używana 😉
No description provided.