我需要为我的一个项目创建自定义关联。我在模型中提供了自定义类名和外键,但仍然出现错误。这是我的代码:
评论模型:
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :portfolio, -> { where( comments: {comment_type: 'p'}) }, :foreign_key => :type_id, :class_name => 'Portfolio'
end
投资组合模型:
class Portfolio < ActiveRecord::Base
has_many :portfolio_details, :dependent => :destroy
has_many :marks, -> { where(mark_type: 'p') }, :foreign_key => :type_id, :dependent => :destroy
has_many :comments, -> { where(comment_type: 'p').order('id DESC') }, :foreign_key => :type_id, :dependent => :destroy
has_many :likes, -> { where(like_type: 'p') }, :foreign_key => :type_id, :dependent => :destroy
has_many :portfolio_files, -> { order 'portfolio_files.order' }, :dependent => :destroy
belongs_to :category
belongs_to :user
end
当我尝试通过评论表中的关联获取投资组合详细信息时。它给出了以下错误:
PG::UndefinedTable: ERROR: missing FROM-clause entry for table "comments"
LINE 1: ...ROM "portfolios" WHERE "portfolios"."id" = $1 AND "comments"... ^
: SELECT "portfolios".* FROM "portfolios" WHERE "portfolios"."id" = $1 AND "comments"."comment_type" = $2 LIMIT 1
现在我如何获得评论的投资组合细节。请建议。 提前谢谢。