[問題] 多張表的串接
我想將三張表串接起來
可是發生了以下的錯誤
請問我有那裡沒有做對嗎
資料表的串接是否有其他的作法
感覺這種方式多張表串接的時候有點麻煩
# 錯誤訊息
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>
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.250.2.32
※ 文章網址: http://www.ptt.cc/bbs/Ruby/M.1410866884.A.22B.html
→
09/16 22:27, , 1F
09/16 22:27, 1F
→
09/16 23:38, , 2F
09/16 23:38, 2F
→
09/17 11:50, , 3F
09/17 11:50, 3F
→
09/17 11:50, , 4F
09/17 11:50, 4F
→
09/17 21:39, , 5F
09/17 21:39, 5F
→
09/18 09:49, , 6F
09/18 09:49, 6F
→
09/18 09:50, , 7F
09/18 09:50, 7F
→
09/18 09:51, , 8F
09/18 09:51, 8F
→
09/21 23:06, , 9F
09/21 23:06, 9F
→
09/21 23:07, , 10F
09/21 23:07, 10F
→
09/21 23:08, , 11F
09/21 23:08, 11F
→
09/21 23:08, , 12F
09/21 23:08, 12F
Ruby 近期熱門文章
PTT數位生活區 即時熱門文章