Trying to create a nested class' show - getting the error 'ActiveRecord::StatementInvalid in StocksController#show Could not find table 'time_deltas''

StackOverflow https://stackoverflow.com/questions/23119464

Question

I'm trying to follow along the Rails getting started guide except for the fact that I'm using Stocks instead of articles and TimeDeltas instead of comments. Now when I go to the view I keep getting this error

ActiveRecord::StatementInvalid in StocksController#show Could not find table 'time_deltas'

on line: @time_delta = @stock.time_deltas.build

Heres my view:

<h1> Stock </h1>
<table> 
    <tr>
        <th>Stock</th>
        <th>Hashtag</th>        
    </tr>
        <tr>
            <td><%= @stock.name %></td>
            <td><%= @stock.hashtag %></td>
        </tr>
</table>

<h2>Deltas: </h2>
  <table> 
    <tr>
      <th>Stock</th>
      <th>Hashtag</th>    
    </tr>
  <% @stock.deltas.each do |delta| %>
    <tr>
      <td><%= @delta.start %></td>
      <td><%= @delta.length %></td>
    </tr>
  <% end %>
</table>

<h2>Add a TimeDelta:</h2>
<%= form_for([@stock, @stock.time_deltas.build]) do |f| %>
  <p>
    <%= f.label :start %><br>
    <%= f.text_field :start %>
  </p>
  <p>
    <%= f.label :length %><br>
    <%= f.text_area :length %>
  </p>
  <p>
    <%= f.submit %>
  </p>
<% end %>

<%= link_to 'Back', stocks_path%>
<%= link_to 'Edit', edit_stock_path(@stock)%>

Heres my stocks controller:

class StocksController < ApplicationController
    def new
        @stock = Stock.new
    end

    def index
        @stocks = Stock.all
    end

    def create
        # XXX Add columns for delta and current standing when we get there
        # they can intiate to nil
        @stock = Stock.new(stock_params)
        if @stock.save
            redirect_to @stock  
        else
            render 'new'
        end
    end

    def show
        @stock = find_stock
        @time_delta = @stock.time_deltas.build
    end

    def edit
        @stock = find_stock
    end

    def update
        @stock = find_stock


        if @stock.update(stock_params)
            redirect_to @stock
        else
            render 'edit'
        end
    end

    def destroy
        @stock = find_stock
        @stock.destroy

        redirect_to stocks_path
    end

    private 
        def stock_params
            params.require(:stock).permit(:name, :hashtag)
        end

        def find_stock
      return Stock.find(params[:id])
    end
end

Heres my TimeDelta controller

class TimeDeltasController < ApplicationController
  def new
    @stock = Stock.find(params[:stock_id])
    @time_delta = @stock.time_deltas.build
    # respond_with(@stock, @time_delta)
  end
  def create
    @stock = Stock.find(time_delta_params)
    @time_delta = @stock.time_deltas.create(params[:stock])
    redirect_to stock_path(@stock)
  end

  private 
    def time_delta_params
      params.require(:time_delta).permit(:start, :length)
    end
end

I've tried re running my migrations as well and to no-avail, any suggestions?

EDIT: Both models Stock:

class Stock < ActiveRecord::Base
    has_many :deltas
    has_many :time_deltas
    validates :hashtag, :name , presence: true, :uniqueness => true, length: { minimum: 2 }
end

Time Delta:

class TimeDelta < ActiveRecord::Base
  belongs_to :stock
end

Database Schema:

# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20140412204548) do

  create_table "delta", force: true do |t|
    t.datetime "start"
    t.integer  "length"
    t.integer  "stock_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "delta", ["stock_id"], name: "index_delta_on_stock_id"

  create_table "queries", force: true do |t|
    t.text     "tweet"
    t.integer  "tweetId"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "stocks", force: true do |t|
    t.text     "name"
    t.text     "hashtag"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "time_delta", force: true do |t|
    t.datetime "start"
    t.integer  "length"
    t.integer  "stock_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "time_delta", ["stock_id"], name: "index_time_delta_on_stock_id"

end
Was it helpful?

Solution

When you created your table via migration the name of your "time_deltas" table was actually created as "time_delta". Keeping the database name in singular form could bring up troubles in the future. The best suggestion is to rename your database to "time_deltas" and it'd work just fine. Also, i noticed that there's another table with a singular name form (delta). I suggest you rename it to "deltas".

Happy coding!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top