我想将三张表串接起来
可是发生了以下的错误
请问我有那里没有做对吗
资料表的串接是否有其他的作法
感觉这种方式多张表串接的时候有点麻烦
# 错误讯息
http://ppt.cc/nev0
#schema.rb
ActiveRecord::Schema.define(version: 20140916095516) do
create_table "categories", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "histories", force: true do |t|
t.integer "item_id"
t.integer "price"
t.date "expend_at"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "items", force: true do |t|
t.integer "category_id"
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
end
# model
class History < ActiveRecord::Base
belongs_to :item
belongs_to :category
end
class Item < ActiveRecord::Base
belongs_to :category
has_many :history
end
class Category < ActiveRecord::Base
has_many :item
has_many :history, :through => :item
end
# controller
class HistoryController < ApplicationController
def index
@histories = History.joins(:item).joins(:category).all
end
end
#view
<tbody>
<% @histories.each do |history| %>
<tr>
<td><%= history.id %></td>
<td><%= history.category.name %></td>
<td><%= history.item.name %></td>
<td><%= history.price %></td>
<td><%= history.expend_date %></td>
<td><%= history.created_at %></td>
<td><%= history.updated_at %></td>
</tr>
<% end %>
</tbody>