【发布时间】:2020-08-05 01:49:32
【问题描述】:
我有以下三个模型(每个字段在 # 之后):
class User < ApplicationRecord
has_many :user_tags, dependent: :destroy
end
class UserTag < ApplicationRecord
belongs_to :user
end
class JourneyTag < ApplicationRecord
belongs_to :journey
end
我想查找所有带有与用户标签相对应的标签的旅程 - user.user_tags。如何获取这些数据?
我想做什么:
current_user.user_tags.each do |user_tag|
JourneyTag.where(cms_tag_id: user_tag.cms_tag_id)
end
这应该给我与user.user_tags 相同的cms_tag_id 的JourneyTag,然后我想抓住这个JourneyTag 的集合来查找Journey。但是上面的查询没有用,因为它总是返回一些 JourneyTag,即使它与 user_tag.cms_tag_id 不匹配也是如此。
【问题讨论】:
-
为了调试,您可以将这些行添加到
puts user_tag.cms_tag_id; puts JourneyTag.where(cms_tag_id: user_tag.cms_tag_id).to_sql上方的块查询中 -
您忘记添加旅程模型了。
标签: sql ruby-on-rails ruby