ActiveRecord::HasManyThroughAssociationNotFoundError 根据文档解决所有问题
ActiveRecord::HasManyThroughAssociationNotFoundError issue with everything as per the Docs
以下是我的模型:
class Client < ApplicationRecord
has_many :client_authorization_rules
has_many :projects, through: :client_authorization_rule
end
class Project < ApplicationRecord
has_many :client_authorization_rules
has_many :clients, through: :client_authorization_rule
end
class ClientAuthorizationRule < ApplicationRecord
belongs_to :project
belongs_to :client
validates_presence_of :project
validates_presence_of :client
end
所以,我已经按照文档进行了所有操作:http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association
我得到的错误是当我做类似的事情时:
p.clients
这里"p"是一个项目。如果不出现以下错误,我什至无法访问项目的 "clients" 方法:
ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association :client_authorization_rule in model Project
不确定我错过了什么。谢谢。我的 Rails 版本是 Rails 5.0.6 和 ruby 2.2.3。这是一些已知问题吗?我找不到任何有这个问题的人。
through: :client_authorization_rule
应该是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
当您在 rails 5 中写入 belongs_to :project
时,还有一件事作为旁注,它自动意味着在保存之前需要 project_id
。所以没有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
关注
class Client < ApplicationRecord
has_many :client_authorization_rules
has_many :projects, through: :client_authorization_rule
end
class Project < ApplicationRecord
has_many :client_authorization_rules
has_many :clients, through: :client_authorization_rule
end
应该是
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
注意:- through: :client_authorization_rule
应该是 through: :client_authorization_rules
以下是我的模型:
class Client < ApplicationRecord
has_many :client_authorization_rules
has_many :projects, through: :client_authorization_rule
end
class Project < ApplicationRecord
has_many :client_authorization_rules
has_many :clients, through: :client_authorization_rule
end
class ClientAuthorizationRule < ApplicationRecord
belongs_to :project
belongs_to :client
validates_presence_of :project
validates_presence_of :client
end
所以,我已经按照文档进行了所有操作:http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association
我得到的错误是当我做类似的事情时:
p.clients
这里"p"是一个项目。如果不出现以下错误,我什至无法访问项目的 "clients" 方法:
ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association :client_authorization_rule in model Project
不确定我错过了什么。谢谢。我的 Rails 版本是 Rails 5.0.6 和 ruby 2.2.3。这是一些已知问题吗?我找不到任何有这个问题的人。
through: :client_authorization_rule
应该是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
当您在 rails 5 中写入 belongs_to :project
时,还有一件事作为旁注,它自动意味着在保存之前需要 project_id
。所以没有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
关注
class Client < ApplicationRecord
has_many :client_authorization_rules
has_many :projects, through: :client_authorization_rule
end
class Project < ApplicationRecord
has_many :client_authorization_rules
has_many :clients, through: :client_authorization_rule
end
应该是
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
注意:- through: :client_authorization_rule
应该是 through: :client_authorization_rules