Rails: Hasmanythroughassociationnotfounderror

ActiveRecord::HasManyThroughAssociationNotFoundError

Add this to User class

has_many :collected_posts, through: :collections, source: :posts

HasManyThroughAssociationNotFoundError in Users#show

Your comment is way off, and the linked documentation has nothing to do with your problem.

The problem with your code is that you have a brutally obvious typo in several places. Your association is called associations, but your :through uses accociations.

Associations vs ACCociations.

Rails is telling you exactly what the error is:

Could not find the association :accociations in model User

The reason your linked documentation fixed your problem is that you probably spelled the new association name correctly. It's pretty important that you spell things correctly when programming, and such an obvious typo should really leap out at you.

ActiveRecord::HasManyThroughAssociationNotFoundError issue with everything as per the Docs

through: :client_authorization_rule

it should be client_authorization_rules

class Client < ApplicationRecord

has_many :client_authorization_rules
has_many :projects, through: :client_authorization_rules

end

class Project < ApplicationRecord

has_many :client_authorization_rules
has_many :clients, through: :client_authorization_rules

end

one more thing as a side note when you write belongs_to :project in rails 5 it automatically means thats project_id is required before save. so there is no sense of validates_presence_of :project

class ClientAuthorizationRule < ApplicationRecord

belongs_to :project #it means that project_id required is true
belongs_to :client #it means that client_id required is true

#validates_presence_of :project
#validates_presence_of :client

end

Could not find the association problem in Rails

In the ApplicationForm class, you need to specify ApplicationForms's relationship to 'form_questions'. It doesn't know about it yet. Anywhere you use the :through, you need to tell it where to find that record first. Same problem with your other classes.

So

# application_form.rb
class ApplicationForm < ActiveRecord::Base
has_many :form_questions
has_many :questions, :through => :form_questions
end

# question.rb
class Question < ActiveRecord::Base
belongs_to :section
has_many :form_questions
has_many :application_forms, :through => :form_questions
end

# form_question.rb
class FormQuestion < ActiveRecord::Base
belongs_to :question
belongs_to :application_form
belongs_to :question_type
has_many :form_questions_answers
has_many :answers, :through => :form_question_answers
end

That is assuming that's how you have it set up.

Rails 4 Could not find the association has_many, through: relationship error

You need to add has_many :album_features both to Album and Feature models (given that you rename JoinTable1 model to more meaningful AlbumFeature, and corresponding table would be album_features), as :through references association - your error is exactly about it.

Or you can use has_and_belongs_to_many so there will be no need to define special link model. But in that case you must name your table albums_features.

Many to many relationship ActiveRecord::HasManyThroughAssociationNotFoundError

Your models' associations should be set up like this:

# count.rb
class Count < ApplicationRecord
has_many :counts_users
has_many :users, through: :counts_users
end

# user.rb
class User < ApplicationRecord
has_many :counts_users
has_many :counts, through: :counts_users
end

# counts_user.rb
class CountsUser < ApplicationRecord
belongs_to :user
belongs_to :count
end

See: the Rails Guides on has_many :through Association



Related Topics



Leave a reply



Submit