title: Trying create a subclass object from the parent objects controller
body: <p>I'm trying to figure out how I can grab the current instance of an object called "meal" to create a food object that belongs to it. </p>
  
  <p>It works in console... its simple and goes like this:</p>
  
  <pre><code>user = User.first
  user.meal.first.meal_foods.create
  </code></pre>
  
  <p>A user can have many meals, and each meal can have many meal_foods. </p>
  
  <p>The challenge here for me is in the create action of the foods controller in creating my meals_food. </p>
  
  <p>(I'm using STI here hence the "foods" controller and the "meal_food" name)</p>
  
  <p>Currently the create action looks like this:</p>
  
  <pre><code> @food = current_user.meal.meal_foods.build 
  </code></pre>
  
  <p>I've also tried this because something pluralizing the objects name works</p>
  
  <pre><code> @food = current_user.meal.meal_food.build 
  </code></pre>
  
  <p>Here's the error it gives for both</p>
  
  <pre><code>undefined method `meal_foods' for [#&lt;Meal id: 17, user_id: 1, meal_name: "Meal"&gt;]:ActiveRecord::Relation
  </code></pre>
  
  <hr>
  
  <p><strong>Update:</strong></p>
  
  <p>The issue I'm having here specifically is selecting the correct meal to create a meal_food for. </p>
  
  <p>In console I can select the first one, which is fine. But in the foods controller, I need to <em>select the correct meal</em> for which to create a meal_food. </p>
  
  <p>Writing meals.first would select the very first meal for that user. If I want to select the 3rd meal out of 5, I need to figure a way to grab that meal's id. </p>
  
  <p>I've just tried this:</p>
  
  <pre><code>&lt;%= link_to "new food", foods_path(id: meal.id), method: :create %&gt;
  </code></pre>
  
  <p>to pass in the meal.id as a parameter that can be used in foods_controller. And then in the foods_controller I did:</p>
  
  <pre><code>@meal_food = current_user.meals.find_by_id(params[:id]).meal_foods.build 
  </code></pre>
  
  <p>It <em>looks</em> like it's submitting because the page is reloaded with a success message, but the meal_food isn't created, so it just doesn't show up. </p>
  
  <p>I checked in console, and there's no new food being created for this users first meal. </p>
  
  <hr>
  
  <p>Ok I realized that the link_to I wrote above creates this url:</p>
  
  <pre><code>foods?id=29
  </code></pre>
  
  <p>and the method I'm using to retrieve this id isn't working because params[:id] is looking for a path id, not this url id. </p>
  
  <p>Thanks in advance!</p>
  
  <hr>
  
  <p>The Models:</p>
  
  <pre><code>class Meal &lt; ActiveRecord::Base
    before_save :sanitize
    has_and_belongs_to_many :meal_foods
    attr_accessible :meal_name
  
    def sanitize
      self.meal_name = "Meal"
    end
  end 
  
  
  class Food &lt; ActiveRecord::Base
    attr_accessible :brand, :carbs, :fat, :name, :protien, :type
  
  end   
  
  class MealFood &lt; Food
    has_and_belongs_to_many :meals
  end 
  
  class User &lt; ActiveRecord::Base
  
    has_many :meal, dependent: :destroy
    has_many :custom_foods, dependent: :destroy 
  </code></pre>
  
  <p>The controllers:</p>
  
  <pre><code>class FoodsController &lt; ApplicationController
    #functions
    def create
  
      #this is where I need to grab the correct meal, and then create a meal_food for it...
  
      if @meal_food.save!
        flash[:success] = "Food created successfully!"
        redirect_to meal_path(current_user.id)
      else
        flash[:error] = "Food couldn't be created."
        redirect_to meal_path(current_user.id)
      end
   end
  end 
  </code></pre>
  
  <p>Partials:</p>
  
  <p>Here's the meal partial that gets repeated to display each meal. It has the link_to for creating a meal that will belong to the meal it's under. </p>
  
  <pre><code>&lt;tr&gt;
    &lt;thead class=meal-thead&gt;
     &lt;td class=meal-thead-name&gt; &lt;%= meal.meal_name %&gt; &lt;/td&gt;
       &lt;th&gt; &lt;/th&gt;
       &lt;th&gt; &lt;/th&gt;
       &lt;th&gt; &lt;/th&gt;
       &lt;th&gt; &lt;/th&gt;
       &lt;th&gt; &lt;%= link_to "x", meal_path(meal.id), method: :delete %&gt; &lt;/th&gt;
    &lt;/thead&gt;
  
    &lt;tbody class=meal-tbody&gt;
  
    &lt;%# get reference to current meal and its foods %&gt;
    &lt;%= render meal.meal_foods %&gt;
  
    &lt;td class=remove-td-center&gt; &lt;%= link_to "new food", foods_path, method: :create %&gt; &lt;/td&gt; 
   &lt;/tbody&gt;    
  &lt;/tr&gt; 
  </code></pre>