我有多个关联:关联名字找不到,也许你拼错了吗?
User
class User < ActiveRecord::Base
has_one :family_tree, dependent: :destroy
has_many :memberships, dependent: :destroy
has_many :nodes, dependent: :destroy
has_many :participants, dependent: :destroy
has_many :comments
Node
class Node < ActiveRecord::Base
include PublicActivity::Model
tracked except: :update, owner: ->(controller, model) { controller && controller.current_user }
belongs_to :family_tree
belongs_to :user
belongs_to :media, polymorphic: true, dependent: :destroy
has_many :comments, dependent: :destroy
has_many :node_comments, dependent: :destroy
Notification
class Notification < PublicActivity::Activity
acts_as_readable :on => :created_at
end
我现在用的是public_activity宝石,当我做这在我ApplicationController
:
@unread_act = Notification.where(owner_id: current_user).includes(trackable: { user: :node}).unread_by(current_user)
我得到这个错误:
ActiveRecord::AssociationNotFoundError at/
Association named 'node' was not found on User; perhaps you misspelled it?
即使当我尝试这样做香草版本:
@unread_act = Notification.where(owner_id: current_user).includes(:node).unread_by(current_user)
我得到这个错误:
ActiveRecord::AssociationNotFoundError at/
Association named 'node' was not found on Notification; perhaps you misspelled it?
有什么事情是造成这个?
编辑1
,当我试图只是做了includes(:trackable)
,因为我使用的子弹宝石,试图追查N + 1个错误,我得到这个消息:
N+1 Query detected
Node => [:user]
Add to your finder: :include => [:user]
N+1 Query detected
Node => [:media]
Add to your finder: :include => [:media]
N+1 Query method call stack
因此,我所做的一切都试图减少这些N + 1查询问题。
你试过使用复数节点吗?因为这就是它的定义在你的has_many – CambridgeMike 2014-11-05 22:08:19
是......我也试过:'ActiveRecord :: AssociationNotFoundError at/ 在Notification上找不到名为'nodes'的关联;也许你拼错了吗?' – marcamillion 2014-11-06 11:05:05