通过外部表变量rails 6进行过滤联接查询

时间:2019-12-02 21:38:27

标签: ruby-on-rails activerecord

我是Rails的新手,我试图弄清楚如何建立关系。

我有2个模型

class Story < ActiveRecord::Base
  validates :text, presence: true, length: { minimum: 10, maximum: 3000 }
  has_many :users, inverse_of: :story, through: :favorite_stories
  belongs_to :topic, inverse_of: :stories
  belongs_to :user
  scope :language, ->(l) { where(language: l) }
end
 class Topic < ActiveRecord::Base
   has_many :stories, inverse_of: :topic
 end

在graphql查询中,我需要过滤掉没有给定语言的故事。

module Types
  class QueryType < Types::BaseObject
    field :topic, Types::TopicType, null: false, description: "Get a topic with stories" do
      argument :id, ID, required: true
      argument :language, Types::Languages, required: true
    end

    def topic(id:, language:)
      # doesn't work 
      # Topic.includes(:stories).merge(Story.language(language)).find id

      #it works but doesn't filter out stories
      Topic.includes(:stories).find id
    end
  end
end

基本上,我需要的是单个主题作为输出,所有故事都按语言过滤。

0 个答案:

没有答案
相关问题