【发布时间】:2015-06-18 16:52:28
【问题描述】:
我想在 User 和 Task 模型之间创建一对多关联。在我的用户模型中,我想给它一个别名owner,并将用户的任务称为owned_tasks。
class User < ActiveRecord::Base
has_many :owned_tasks, class_name: "Task", as: :owner
end
class Task < ActiveRecord::Base
#owner_id
belongs_to :owner, class_name: "User"
end
当我尝试检索任务列表时,我遇到了这个错误:
user = User.first
user.owned_tasks
SQLite3::SQLException: no such column: tasks.owner_type: SELECT "tasks".* FROM "tasks" WHERE "tasks"."owner_id" = ? AND "tasks"."owner_type" = ?
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: tasks.owner_type: SELECT "tasks".* FROM "tasks" WHERE "tasks"."owner_id" = ? AND "tasks"."owner_type" = ?
当我的数据库中没有存储该名称的属性时,为什么它指的是owner_type?
【问题讨论】:
-
删除
as: :owner... 为什么需要它?你打算做polymorphicassociations 吗? -
没找到你?你的意思是 ? ;)
-
使用
task.owner时不也返回信息吗? -
一切都会好起来的..只需删除
as:的东西.. -
删除
as:后我收到此错误:no such column: tasks.user_id
标签: ruby-on-rails activerecord associations has-many