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

Igor Magalhaes Week 4 IC #11

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

IgorrMagalhaess
Copy link

Hey guys, I had a good time doing this over the weekend and forgot to submit it yesterday. I'd appreciate if I could get some feedback. Thank you!

redirect_to dish_path(params[:id])
end

def destroy
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice work making it to the extension! Great use of find_by

@@ -0,0 +1,6 @@
class IngredientsController < ApplicationController
def index
Copy link
Contributor

@juliet-e juliet-e Feb 29, 2024

Choose a reason for hiding this comment

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

This is an okay controller option for US 3, but you could also consider a nested controller: chefs/ingredients_controller since this is a list of ingredients just for 1 specific chef

@@ -1,4 +1,5 @@
class Chef < ApplicationRecord
validates_presence_of :name
has_many :dishes
has_many :ingredients, through: :dishes
Copy link
Contributor

Choose a reason for hiding this comment

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

Even though distinct makes a small difference on the query, by pure MVC principles, it's beyond the basic list of CRUD methods in AR so it could go in a model method here. Nice work getting the additional association to ingredients to make it easier!

def ingredient_list
   self.ingredients.distinct.pluck(:name) # plucking is optional
end

has_many :ingredients, through: :ingredient_dishes

def total_calories
ingredients.sum(:calories)
Copy link
Contributor

Choose a reason for hiding this comment

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

Awesome

<% @dish.ingredients.each do |ingredient| %>
<div id="ingredient_<%= ingredient.id %>">
<li><%= ingredient.name %></li>
<%= form_with model: @dish, url: destroy_ingredient_dish_path(@dish), method: :delete do |form| %>
Copy link
Contributor

Choose a reason for hiding this comment

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

form_with model is just another way to set up your forms, but I generally think it's best only when you're using it to create or edit a whole object, rather than doing something like creating a joins record. It can sometimes complicate things more for forms where you're doing something different (like in this case) so just be careful! You don't have to use it, it's just another tool in your toolbox

delete "/dishes/:id/ingredients", to: "ingredient_dishes#destroy", as: :destroy_ingredient_dish

resources :chefs, only: [:show] do
resources :ingredients, only: [:index]
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice RESTful routes! For this one, if you wanted to direct this route to a nested controller, you could add controller: "chefs/ingredients" to the end of line 12

@@ -8,6 +8,7 @@

describe "relationships" do
it {should have_many :dishes}
it {should have_many(:ingredients).through(:dishes)}
Copy link
Contributor

Choose a reason for hiding this comment

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

Great work remembering to add a test for this new association!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants