Rails:内部连接与 act-as-taggable-on's find_lated_tags 给出 mysql 错误
我在 Rails 3 的 ActiveRecord 中设置了以下结构,
class Domain < AR
has_and_belongs_to_many :videos
end
class Video < AR
has_and_belongs_to_many :domains
acts_as_taggable_on :tags
scope :with_state, lambda { |s| where('state = ?', s) }
end
在我的控制器中的某个位置,我想根据某个标签从视频中查找其他视频,使用 find_lated_tags 方法非常容易。但我需要将相关视频限制为当前域,该域通过名为 current_domain
的帮助程序方法呈现,因此我提出了以下 AR 查询:
@video = Video.find(params[:id])
@video.find_related_tags.joins(:domains).with_state(:converted).where('domains.id = ?', current_domain.id)
以下 MySQL 查询:
SELECT videos.*, COUNT(tags.id) AS count FROM videos, tags, taggings
INNER JOIN `domains_videos` ON `domains_videos`.`video_id` = `videos`.`id`
INNER JOIN `domains` ON `domains`.`id` = `domains_videos`.`domain_id`
WHERE (videos.id != 5 AND videos.id = taggings.taggable_id
AND taggings.taggable_type = 'Video'
AND taggings.tag_id = tags.id
AND tags.name IN ('not'))
AND (state = 'converted')
AND (domains.id = 4)
GROUP BY videos.id
ORDER BY count DESC
此 AR 查询生成 对我来说很好,但它会产生 MySQL 错误:
Mysql2::Error: Unknown column 'videos.id' in 'on clause'
我不明白! 中的未知列
INNER JOIN `domains_videos` ON `domains_videos`.`video_id` = `videos`.`id`
为什么 videos.id 是“所有连接表均已正确设置” 。一切正常。如果我删除 .joins(:domains)
部分,它就会起作用......我确信这是一些简单的事情,我只是没有看到:)
I've got the following structure set up in ActiveRecord in Rails 3
class Domain < AR
has_and_belongs_to_many :videos
end
class Video < AR
has_and_belongs_to_many :domains
acts_as_taggable_on :tags
scope :with_state, lambda { |s| where('state = ?', s) }
end
Somewhere in my controller I want to find other videos from a video based on a certain tag, wich is quite easy using the find_related_tags method. But I need to restrict the related videos to the current domain which is present through a helper method called current_domain
, so I came up with the following AR query:
@video = Video.find(params[:id])
@video.find_related_tags.joins(:domains).with_state(:converted).where('domains.id = ?', current_domain.id)
This AR query produces the following MySQL query:
SELECT videos.*, COUNT(tags.id) AS count FROM videos, tags, taggings
INNER JOIN `domains_videos` ON `domains_videos`.`video_id` = `videos`.`id`
INNER JOIN `domains` ON `domains`.`id` = `domains_videos`.`domain_id`
WHERE (videos.id != 5 AND videos.id = taggings.taggable_id
AND taggings.taggable_type = 'Video'
AND taggings.tag_id = tags.id
AND tags.name IN ('not'))
AND (state = 'converted')
AND (domains.id = 4)
GROUP BY videos.id
ORDER BY count DESC
It seems fine to me, but it produces a MySQL error:
Mysql2::Error: Unknown column 'videos.id' in 'on clause'
And that I don't get! Why is videos.id an unkown column in
INNER JOIN `domains_videos` ON `domains_videos`.`video_id` = `videos`.`id`
All join tables are properly set up. Everything works. If I remove the .joins(:domains)
part it works... I'm sure it's something simple I'm just not seeing :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来MySQL查询是有效的,但videos.id列未知,因为videos不在连接集中,而仅在FROM子句中使用。所以我最终得到了执行以下操作的解决方案:
由于现在所有内容都已真正连接,因此该语句现在对 MySQL 有效。
It seems that the MySQL query is valid, but the videos.id column is unknown cause videos is not in the join set but only used in the FROM clause. So I ended up with the solution to do the following:
Since everything is really joined now, the statement now is valid for MySQL.