Ruby on rails 用于定义作用域的多态关联和联接语法

Ruby on rails 用于定义作用域的多态关联和联接语法,ruby-on-rails,activerecord,polymorphic-associations,Ruby On Rails,Activerecord,Polymorphic Associations,我试图定义一个引用多态关联上属性“state”的范围 我的现实世界的问题是,我的文章和事件涉及到特定的项目。我将这些捕获为项_引用。我的文章和事件可以是草稿或已发布状态。我希望能够在发布关联(item_referenceable)状态的地方提取item_引用 大概是这样的: scope:published\u resource,->{where(“item\u referenceable.published?”)} 但这显然不起作用,因为item\u referenceable是一个实例方法,我

我试图定义一个引用多态关联上属性“state”的范围

我的现实世界的问题是,我的文章和事件涉及到特定的项目。我将这些捕获为项_引用。我的文章和事件可以是草稿或已发布状态。我希望能够在发布关联(item_referenceable)状态的地方提取item_引用

大概是这样的:

scope:published\u resource,->{where(“item\u referenceable.published?”)}

但这显然不起作用,因为
item\u referenceable
是一个实例方法,我在类级别工作。我想我需要去加入前线,但我正在努力想办法

以下是我代码的相关部分:

型号:

class Article < ApplicationRecord
  has_many :item_references, as: :item_referenceable
  accepts_nested_attributes_for :item_references, allow_destroy: true,
   :reject_if => proc { |att| att[:item_unique_id].blank? }
end

class Event < ApplicationRecord
  has_many :item_references, as: :item_referenceable
  accepts_nested_attributes_for :item_references, allow_destroy: true,
   :reject_if => proc { |att| att[:item_unique_id].blank? }
end

class ItemReference < ApplicationRecord
  belongs_to :item_referenceable, polymorphic: true

  scope :in_articles, -> { where("item_referenceable_type = 'Article'") }
end
问题很可能会扩大,因为我最终将在多个模型中引用项目,例如课程、博客等。我想知道是否可以定义范围,而不必指定特定的模型名称,并使用项目可引用类型来引用模型名称

以下是我失败的尝试:

scope:published\u resource,->{joins(:item\u referenceable\u type)。其中('item\u referenceable.item\u referenceable.published?')}

我最终尝试生成一个API,它通过item_unique_id显示引用该项的所有文章和事件

像这样:

{
 "title": "Retrieve all item references",
 "items": [
   {
     "item_unique_id": "O12",
     "articles": [
       {
         "resource_id": 1,
         "title": "Penguin",
         "state": "published",
         "slug": "test-article"
       }
      ],
     "events": []
   },

我已经使用
ItemReference.all.group\u by(&:item\u unique\u id)
进入了上述阶段,但它可以检索所有资源(事件/文章),无论它们是否已发布。

以下是我建议的处理方法:(简而言之,扩展功能几乎肯定是一种需求)

然后你就可以打电话了

publishables = Publishable.new
publishables.published!
publishables.limit = 10 
publishables.results 

正如我刚才提到的,这是非常简化的,只是为了给你一个例子,我当然会考虑扩展这个功能,但希望它有助于指向一个方向

恐怕我仍然有点不清楚。首先,状态在哪里,状态是什么数据类型?它是单独存储在
文章
事件
中,还是存储在
项目参考
中。其次,这里的最终目标是什么,能够调用
ItemReferences.published\u参考资料
?啊,很抱歉,这还不清楚。模型事件和项目上有一个属性状态。我可以打电话给你吗?在所有实例上,如果state==“Published”,则返回true。是的,这正是我想要做的。我正在准备一个API,它显示所有引用特定项目的文章和事件,我只想显示已发布的文章和事件,所以
ItemReferences.published\u参考资料
ok。多态关联不一定允许您调用
ItemReferences.published_resources
并返回
Event
Article
对象,但每个项都可以独立执行,例如
scope:published{where(state:'published')}
如果放置在
文章中
将允许您调用
Article.published
我已经可以调用Article.published或Event.published,但问题是我的API需要按项返回所有相关资源。我会修改我的问题,让它更清楚一点。谢谢你的帮助。是的,正如我刚才提到的,多态关联不能做到这一点,因为连接的性质。如果您自己尝试,您将看到以下错误
ActiveRecord::急切地加载多态性错误:无法急切地加载多态性关联:item\u referenceable
。我建议为这个功能构建一个单独的类,并独立地查询每个类。这是我可以提供的一个例子。
class Publishable 
   PUBLISHABLES = {articles: Article, events: Event}
   attr_reader :results
   attr_accessor :limit, :conditions   
   def initialize(conditions={})
      @conditions = conditions 
      @results = {} 
   end   

   def as_json(options={})
     fetch if results.empty?  
     results.as_json(options) 
   end  

   def published!
     conditions.merge!({stage: 'Published'})
     self 
   end 

   private 
     def fetch
       PUBLISHABLES.each do |node, klass| 
         results[node] = klass.where(conditions).limit(limit)
       end  
     end 
end 
publishables = Publishable.new
publishables.published!
publishables.limit = 10 
publishables.results