我有像这样的关系模型
class User < ActiveRecord::Base
has_many :identities
has_many :activities
end
class Identity < ActiveRecord::Base
belongs_to :user
has_many :manifests
has_many :activities, through: :manifests
end
class Activity < ActiveRecord::Base
belongs_to :user
has_one :link
has_many :manifests
has_many :identities, through: :manifests
end
class Manifest < ActiveRecord::Base
belongs_to :activity
belongs_to :identity
end
class Link < ActiveRecord::Base
belongs_to :activity
end
但是当我试着打电话时
user.activities
引发错误
ActiveRecord::AssociationNotFoundError: Association named 'user' was not found on Link; perhaps you misspelled it?
让我感到困惑,因为我没有将用户关系放在Link模型上。有人能帮助我吗?
答案 0 :(得分:0)
You could try:
class User < ActiveRecord::Base
has_many :identities
has_many :activities
has_many :links, through: :activities
end
class Link < ActiveRecord::Base
belongs_to :activity
belongs_to :user, through: :activity
end
Why? In this case we should make complied join tables users
, activities
and links
.