Closed louarnos closed 8 years ago
I talked to Natalie who also has a joined table and she says I need to post to meal Items to accomplish what I'd like to do.
Try it. What happens?
Wait are you trying to update a meal, or the foods that are associated with that meal?
the foods that belong to a meal
This is the post I'm working with now:
TOKEN='BAhJIiU2ZjA5YWUzYmNkMzc4ZjFhZjc5Y2ZjNzU4M2ZjZTcxZQY6BkVG--bd4a857908df5f3bc9015aa3e7b2cf89cb710c79'
curl --include --request POST http://localhost:3000/meal_items \
--header "Authorization: Token token=$TOKEN" \
--header "Content-Type: application/json" \
--data '{
"meal_item" : {
"user_meal_id" : 15,
"food_for_meal_id" : 4
}
}'
This is my meal_items controller
class MealItemsController < ApplicationController
before_action :set_meal_item, only: [:show, :update, :destroy]
# GET /meal_items
# GET /meal_items.json
def index
@meal_items = MealItem.all
render json: @meal_items
end
# GET /meal_items/1
# GET /meal_items/1.json
def show
render json: @meal_item
end
# POST /meal_items
# POST /meal_items.json
def create
@meal_item = MealItem.new(meal_item_params)
if @meal_item.save
render json: @meal_item, status: :created, location: @meal_item
else
render json: @meal_item.errors, status: :unprocessable_entity
end
end
# PATCH/PUT /meal_items/1
# PATCH/PUT /meal_items/1.json
def update
@meal_item = MealItem.find(params[:meal_id])
if @meal_item.update(meal_item_params)
render json: @meal_item, status: :ok
else
render json: @meal_item.errors, status: :unprocessable_entity
end
end
# DELETE /meal_items/1
# DELETE /meal_items/1.json
def destroy
@meal_item.destroy
head :no_content
end
private
def set_meal_item
@meal_item = MealItem.find(params[:id])
end
def meal_item_params
params.require(:meal_item).permit(:food_id, :meal_id, :food_for_meal_id, :user_meal_id, :serv_qty)
end
end
server says:
Started POST "/meal_items" for ::1 at 2016-05-03 09:58:15 -0400
Processing by MealItemsController#create as */*
Parameters: {"meal_item"=>{"user_meal_id"=>15, "food_for_meal_id"=>4}}
(0.1ms) BEGIN
SQL (1.0ms) INSERT INTO "meal_items" ("food_for_meal_id", "user_meal_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["food_for_meal_id", 4], ["user_meal_id", 15], ["created_at", "2016-05-03 13:58:15.019165"], ["updated_at", "2016-05-03 13:58:15.019165"]]
(0.5ms) COMMIT
[active_model_serializers] Rendered MealItemSerializer with ActiveModel::Serializer::Adapter::Json (4.78ms)
Completed 500 Internal Server Error in 15ms (ActiveRecord: 1.6ms)
NoMethodError (undefined method `food' for #<MealItem:0x007fb0cbf8ab50>):
app/controllers/meal_items_controller.rb:24:in `create'
I'm confused about the best approach at this point.
can you just updated the foods directly that are associated with that meal instead?
i tried that above. Thats what I was originally doing.
It looks like you were trying to update meals. Not foods
Oh I see what you're asking. Foods can belong to many meals through meal items tho.
yea, but each meal will have foods associated with them. And those foods have an id, so if you update that food then it should be updated when you look for it through a meal
Foods have no foreign key
They are still associated through the join table tho
So you want me to patch what exactly? I'm confused.
I've tried patching meal and meal_items... I'm not sure how to patch foods to accomplish this.
ill assign myself
needed to change meal_item serializer to match the names in the table. Posting to meal_items is now working.
@RDegnen help.
I have a joined table, meal_items, that links meals and foods. I want to patch the meals associated with a users given meal. Meals sees the foods for a given meal as food_for_meals. This is my curl request:
This is my controller for meals:
This is my meal model:
I have tried patching with both meal_items and food_for_meals as the key and I get the same response from the console.