I want to create a one to many association between a User
and Task
model. In my user model I'd like to give it the alias of owner
and refer to the user's tasks as 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
When I try and retrieve the list of tasks as so I run into this error:
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" = ?
Why is it referring to owner_type
when there's no attribute of that name stored in my database?
Here is your corrected version :
Why need
:foreign_key
option ?