Reputation: 53
I'm having a bit of trouble getting the associations worked out between three models - Team, User, and Request.
Here are the models as I Currently have them:
class User < ActiveRecord::Base
belongs_to :team
has_many :created_requests, as: :creator, class_name: "Request"
has_many :assigned_requests, class_name: "Request", as: :assignable, foreign_key: "assignable_id"
end
class Team < ActiveRecord::Base
has_many :users
has_many :assigned_requests, class_name: "Request", as: :assignable, foreign_key: "assignable_id"
end
class Request < ActiveRecord::Base
belongs_to :creator, class_name: "User"
belongs_to :assignable, polymorphic: true
end
Most of the relations seem to work, but I run into a problem trying to view a user's created_requests. The error that I get is "ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column requests.creator_type does not exist"
There is no creator_type since the relationship between users and created_requests isn't polymorphic - only the relationship between users/teams and assigned_requests is polymorphic. How would I specify this in the model?
Thanks!
Upvotes: 2
Views: 585
Reputation: 880
Drop the as: :creator
from the has_many :created_requests, as: :creator, class_name: "Request"
in the User
model. The as:
is only for polymorphic associations, and if it's not a polymorphic association as you say, then you shouldn't have that in there.
Upvotes: 2