【发布时间】:2015-05-07 20:01:57
【问题描述】:
我希望item 能够从ItemType 表中获取关联记录:
item = Item.first
item.item_type # <--- ERROR
但我得到一个错误:
SELECT "item_types".* FROM "item_types" WHERE "item_types"."item_id" = ? LIMIT 1 [[nil, 11]] SQLite3::SQLException: no such column: item_types.item_id: SELECT "item_types".* FROM "item_types" WHERE "item_types"."item_id" = ?限制 1 ActiveRecord::StatementInvalid: SQLite3::SQLException:没有这样的列:item_types.item_id:SELECT "item_types".* FROM "item_types" WHERE "item_types"."item_id" = ? 限制 1 个
从错误中我可以看到 ActiveModel 尝试访问 item_id 列。
但我不想在我的ItemType 表中创建一个item_id 列...它是一个枚举表,其中包含以下项目类型:
#id|name
1|"Task"
2|"User Story"
3|"Bug"
4|"Feature Request"
物品模型
#
# Table name: items
#
# id :integer not null, primary key
# name :string
# created_at :datetime not null
# updated_at :datetime not null
# item_type_id :integer
#
class Item < ActiveRecord::Base
has_one :item_type
end
ItemType 模型
# == Schema Information
#
# Table name: item_types
#
# id :integer not null, primary key
# name :string
# created_at :datetime not null
# updated_at :datetime not null
#
class ItemType < ActiveRecord::Base
end
【问题讨论】:
标签: ruby-on-rails-4 activerecord