{"action":"created","issue":{"url":"https://api.github.com/repos/ga-wdi-boston/capstone-project/issues/103","repository_url":"https://api.github.com/repos/ga-wdi-boston/capstone-project","labels_url":"https://api.github.com/repos/ga-wdi-boston/capstone-project/issues/103/labels{/name}","comments_url":"https://api.github.com/repos/ga-wdi-boston/capstone-project/issues/103/comments","events_url":"https://api.github.com/repos/ga-wdi-boston/capstone-project/issues/103/events","html_url":"https://github.com/ga-wdi-boston/capstone-project/issues/103","id":145992288,"number":103,"title":"I can not view User and User id in my Food model ( Rails)","user":{"login":"Knguyen21","id":16465193,"avatar_url":"https://avatars.githubusercontent.com/u/16465193?v=3","gravatar_id":"","url":"https://api.github.com/users/Knguyen21","html_url":"https://github.com/Knguyen21","followers_url":"https://api.github.com/users/Knguyen21/followers","following_url":"https://api.github.com/users/Knguyen21/following{/other_user}","gists_url":"https://api.github.com/users/Knguyen21/gists{/gist_id}","starred_url":"https://api.github.com/users/Knguyen21/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Knguyen21/subscriptions","organizations_url":"https://api.github.com/users/Knguyen21/orgs","repos_url":"https://api.github.com/users/Knguyen21/repos","events_url":"https://api.github.com/users/Knguyen21/events{/privacy}","received_events_url":"https://api.github.com/users/Knguyen21/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-04-05T13:39:57Z","updated_at":"2016-04-05T14:07:38Z","closed_at":null,"body":"HI, \r\nWhen I run a curl script to create a food item, it does not automatically associate itself to a user. Do you know what I'm missing??\r\n\r\n I changed the controller so that only current user can create, update, show, and destroy. \r\n```ruby\r\nclass FoodsController < OpenReadController\r\n  before_action :set_food, only: [:show, :update, :destroy]\r\n\r\n  # GET /foods\r\n  # GET /foods.json\r\n  def index\r\n    @foods = Food.all\r\n\r\n    render json: @foods\r\n  end\r\n\r\n  # GET /foods/1\r\n  # GET /foods/1.json\r\n  def show\r\n    render json: @food\r\n  end\r\n\r\n  # POST /foods\r\n  # POST /foods.json\r\n  def create\r\n    @food = Food.new(food_params)\r\n\r\n    if @food.save\r\n      render json: @food, status: :created, location: @food\r\n    else\r\n      render json: @food.errors, status: :unprocessable_entity\r\n    end\r\n  end\r\n\r\n  # PATCH/PUT /foods/1\r\n  # PATCH/PUT /foods/1.json\r\n  def update\r\n    @food = Food.find(params[:id])\r\n\r\n    if @food.update(food_params)\r\n      head :no_content\r\n    else\r\n      render json: @food.errors, status: :unprocessable_entity\r\n    end\r\n  end\r\n\r\n  # DELETE /foods/1\r\n  # DELETE /foods/1.json\r\n  def destroy\r\n    @food.destroy\r\n\r\n    head :no_content\r\n  end\r\n\r\n  private\r\n\r\n    def set_food\r\n      @food = current_user.foods.find(params[:id])\r\n    end\r\n\r\n    def food_params\r\n      params.require(:food).permit(:storage, :disposal, :category, :name, :description, :purchased_date, :expiration_date, :remind_date, :user, :user_id)\r\n    end\r\nend\r\n```\r\n\r\nFood serializer\r\n```ruby\r\nclass FoodSerializer < ActiveModel::Serializer\r\n  attributes :id, :category, :storage, :disposal, :name, :description, :purchased_date, :expiration_date, :remind_date, :user, :user_id\r\nend\r\n```\r\n\r\nuser Serializer \r\n```ruby\r\n#\r\nclass UserSerializer < ActiveModel::Serializer\r\n  attributes :id, :email, :foods\r\nend\r\n```\r\nSchema\r\n```ruby \r\n\r\nActiveRecord::Schema.define(version: 20160404133713) do\r\n\r\n  # These are extensions that must be enabled in order to support this database\r\n  enable_extension \"plpgsql\"\r\n\r\n  create_table \"examples\", force: :cascade do |t|\r\n    t.text     \"text\",       null: false\r\n    t.integer  \"user_id\",    null: false\r\n    t.datetime \"created_at\", null: false\r\n    t.datetime \"updated_at\", null: false\r\n  end\r\n\r\n  add_index \"examples\", [\"user_id\"], name: \"index_examples_on_user_id\", using: :btree\r\n\r\n  create_table \"foods\", force: :cascade do |t|\r\n    t.string   \"storage\",\r\n    t.string   \"disposal\",\r\n    t.string   \"name\",\r\n    t.text     \"description\",\r\n    t.date     \"purchased_date\",\r\n    t.date     \"expiration_date\",\r\n    t.date     \"remind_date\",\r\n    t.datetime \"created_at\",      null: false\r\n    t.datetime \"updated_at\",      null: false\r\n    t.integer  \"user_id\",\r\n    t.string   \"category\",\r\n  end\r\n\r\n  add_index \"foods\", [\"user_id\"], name: \"index_foods_on_user_id\", using: :btree\r\n\r\n  create_table \"users\", force: :cascade do |t|\r\n    t.string   \"email\",           null: false\r\n    t.string   \"token\",           null: false\r\n    t.string   \"password_digest\", null: false\r\n    t.datetime \"created_at\",      null: false\r\n    t.datetime \"updated_at\",      null: false\r\n  end\r\n\r\n  add_index \"users\", [\"email\"], name: \"index_users_on_email\", unique: true, using: :btree\r\n  add_index \"users\", [\"token\"], name: \"index_users_on_token\", unique: true, using: :btree\r\n\r\n  add_foreign_key \"examples\", \"users\"\r\n  add_foreign_key \"foods\", \"users\"\r\nend\r\n```\r\n\r\nroutes\r\n```ruby\r\nRails.application.routes.draw do\r\n  resources :foods, except: [:new, :edit]\r\n  resources :examples, except: [:new, :edit]\r\n  post '/sign-up' => 'users#signup'\r\n  post '/sign-in' => 'users#signin'\r\n  delete '/sign-out/:id' => 'users#signout'\r\n  patch '/change-password/:id' => 'users#changepw'\r\n  resources :users, only: [:index, :show]\r\nend\r\n```\r\n\r\ncurl script create food\r\n```shell\r\ncurl --include --request POST http://localhost:3000/foods \\\r\n  --header \"Authorization: Token token=BAhJIiU2ZjdhZDEyZTRmMGQ0MWY2ZmJjMmNjYzM5YmY5MzQ0ZQY6BkVG--2c0b39b90f44ba1da2b6b54b0c2537afd81dd347\" \\\r\n  --header \"Content-Type: application/json\" \\\r\n  --data '{\r\n    \"food\" : {\r\n    \"storage\" : \"freezer\",\r\n    \"disposal\" : \"donate\",\r\n    \"category\" : \"dessert\",\r\n    \"name\" : \"ice cream\",\r\n    \"description\" : \"cookies and cream\",\r\n    \"purchased_date\" : \"2016-03-21\",\r\n    \"expiration_date\" : \"2016-04-01\",\r\n    \"remind_date\" : \"2016-03-29\"\r\n    }\r\n  }'\r\n```\r\n\r\nlocal host:3000/foods\r\n```json \r\n{\r\n\"foods\": [\r\n{\r\n\"id\": 10,\r\n\"category\": \"dessert\",\r\n\"storage\": \"freezer\",\r\n\"disposal\": \"donate\",\r\n\"name\": \"ice cream\",\r\n\"description\": \"cookies and cream\",\r\n\"purchased_date\": \"2016-03-21\",\r\n\"expiration_date\": \"2016-04-01\",\r\n\"remind_date\": \"2016-03-29\",\r\n\"user\": null,\r\n\"user_id\": null\r\n},\r\n```\r\n\r\nWhy is the user and user_id null?? only current user to show, update, create, and destroy their own foods items. "},"comment":{"url":"https://api.github.com/repos/ga-wdi-boston/capstone-project/issues/comments/205824964","html_url":"https://github.com/ga-wdi-boston/capstone-project/issues/103#issuecomment-205824964","issue_url":"https://api.github.com/repos/ga-wdi-boston/capstone-project/issues/103","id":205824964,"user":{"login":"jrhorn424","id":388761,"avatar_url":"https://avatars.githubusercontent.com/u/388761?v=3","gravatar_id":"","url":"https://api.github.com/users/jrhorn424","html_url":"https://github.com/jrhorn424","followers_url":"https://api.github.com/users/jrhorn424/followers","following_url":"https://api.github.com/users/jrhorn424/following{/other_user}","gists_url":"https://api.github.com/users/jrhorn424/gists{/gist_id}","starred_url":"https://api.github.com/users/jrhorn424/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jrhorn424/subscriptions","organizations_url":"https://api.github.com/users/jrhorn424/orgs","repos_url":"https://api.github.com/users/jrhorn424/repos","events_url":"https://api.github.com/users/jrhorn424/events{/privacy}","received_events_url":"https://api.github.com/users/jrhorn424/received_events","type":"User","site_admin":false},"created_at":"2016-04-05T14:07:38Z","updated_at":"2016-04-05T14:07:38Z","body":"@MattB-GA-Boston It should be the current user.\r\n\r\n> When I run a curl script to create a food item, it does not automatically associate itself to a user.\r\n\r\n@Knguyen21 You have to write the code for that. Nothing automatically happens for associations you need to use `current_user` and association methods somewhere.\r\n\r\n```ruby\r\n  # POST /foods\r\n  # POST /foods.json\r\n  def create\r\n    @food = Food.new(food_params) # 👀 here\r\n```\r\n"}}