我使用acts_as_taggabe
gem
我的新闻模式
has_many :taggings
has_many :tags, through: :taggings
有这个范围:
scope :tag, -> (tag_name) { joins(:tags).where('tag.name = ?', tag_name)}
导致当前错误:
PG::UndefinedTable: ERROR: missing FROM-clause entry for table "tag"
生成此SQL:
SELECT "news".* FROM "news" INNER JOIN "taggings" ON "taggings"."news_id" = "news"."id" INNER JOIN "tags" ON "tags"."id" = "taggings"."tag_id" WHERE (tag.name = 'lorem')):
是什么原因?我该如何修复范围? 提前谢谢!
答案 0 :(得分:2)
表格名称为'tags'而非'tag'。
scope :tag, -> (tag_name) { joins(:tags).where('tags.name = ?', tag_name)}