Rails 4.2奇怪错误在Link上找不到名为'user'的关联

时间:2015-06-26 02:34:10

标签: ruby-on-rails ruby-on-rails-4 rails-activerecord model-associations

我有像这样的关系模型

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模型上。有人能帮助我吗?

1 个答案:

答案 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.