Rails 4.2 strange error Association named 'user' was not found on Link

1.2k views Asked by At

I have models with relationship like this

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

But when I try to call

 user.activities

it raise an error

ActiveRecord::AssociationNotFoundError: Association named 'user' was    not found on Link; perhaps you misspelled it?

It got me confused as I don't put user relationship on Link model. Can someone help me?

1

There are 1 answers

1
Сергій Назаревич On

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.