Sql ActiveRecord有很多到多态有很多

Sql ActiveRecord有很多到多态有很多,sql,ruby-on-rails,ruby,activerecord,relation,Sql,Ruby On Rails,Ruby,Activerecord,Relation,看起来rails仍然不支持这种类型的关系,并通过错误抛出ActiveRecord::HasmanyThroughAssociation多态性 我能做些什么来实现这种关系 我有以下协会: Users 1..n Articles Categories n..n Articles Projects 1..n Articles 这是订阅模式 Subscription 1..1 User Subscription 1..1 Target (polymorphic (Article, Category o

看起来rails仍然不支持这种类型的关系,并通过错误抛出ActiveRecord::HasmanyThroughAssociation多态性

我能做些什么来实现这种关系

我有以下协会:

Users 1..n Articles
Categories n..n Articles
Projects 1..n Articles
这是订阅模式

Subscription 1..1 User
Subscription 1..1 Target (polymorphic (Article, Category or User))
我需要根据用户订阅通过SubscriptionTargetTitle选择文章

我不知道该怎么做

理想情况下,我希望获得关联类的实例

更新1

这里有一个小例子

假设用户_1有4条订阅记录:

s1 = (user_id: 1, target_id: 3, target_type: 'User')
s2 = (user_id: 1, target_id: 2, target_type: 'Category')
s3 = (user_id: 1, target_id: 3, target_type: 'Project')
s4 = (user_id: 1, target_id: 8, target_type: 'Project')
我需要方法Userfeed_articles,它获取属于我订阅的任何目标的所有文章

user_1.feed_articles.order(created_at: :desc).limit(10) 
更新2

我在用户模型中按类型分离文章来源:

  has_many :out_subscriptions, class_name: 'Subscription'

  has_many :followes_users, through: :out_subscriptions, source: :target, source_type: 'User'
  has_many :followes_categories, through: :out_subscriptions, source: :target, source_type: 'Category'
  has_many :followes_projects, through: :out_subscriptions, source: :target, source_type: 'Project'

  has_many :feed_user_articles, class_name: 'Article', through: :followes_users, source: :articles
  has_many :feed_category_articles, class_name: 'Article', through: :followes_categories, source: :articles
  has_many :feed_project_articles, class_name: 'Article', through: :followes_projects, source: :articles
但是,我如何才能在不损失性能的情况下,将feed\u user\u文章与feed\u category\u文章和feed\u project\u文章合并呢

更新3.1

我找到的唯一方法是使用原始SQL连接查询。看起来不错,但我不确定

  def feed_articles
    join_clause = <<JOIN
inner join users on articles.user_id = users.id
inner join articles_categories on articles_categories.article_id = articles.id
inner join categories on categories.id = articles_categories.category_id
inner join subscriptions on
    (subscriptions.target_id = users.id and subscriptions.target_type = 'User') or
    (subscriptions.target_id = categories.id and subscriptions.target_type = 'Category')
JOIN

    Article.joins(join_clause).where('subscriptions.user_id' => id).distinct
  end
这仅适用于用户和类别


它支持作用域和其他特性。唯一让我感兴趣的是:这个问题会导致一些不良影响吗

我认为从数据库性能的角度来看,使用UNION ALL多重查询将比使用多态多重连接更有效。它也将更具可读性。我试着写一个Arel查询作为例子,但它不起作用,我没能使ORDERBY子句正常工作,所以我想你必须通过原始SQL来实现它。除了ORDERBY子句之外,您还可以使用SQL模板来干燥它。

Rails不支持has多个:通过w/多态关联,这是正确的。可以通过在用户类上定义实例方法来模拟这种行为。看起来是这样的:

def articles
  Article.
    joins("join subscriptions on subscriptions.target_id = articles.id and subscriptions.target_type = 'Article'").
    joins("join users on users.id = subscriptions.user_id")
end

所以你基本上想要一个给定用户订阅的所有文章?只有使用ActiveRecord dsl没有标准的解决方案。我的解决方案将以同样的方式处理您的关系,请参阅第二次编辑。有没有一种方法可以用原始SQL实现这一点?我不知道关系代数的规范是sql,看起来它工作得很好,但我不确定它是否正确您使用的是什么RDBMS?关于order by子句,您是否将它放在联合体的末尾?e、 g工会的每一部分不得有一份订单。上次我写联合sql语句时,这让我大吃一惊。注:我上次写这篇文章是在oracle SQL中,我不知道postgresql og中的行为是否有所不同mysql@OleHenrikSkogstrøm是的。根据SQL标准,ORDER BY子句必须放在所有联合之后。但阿雷尔不能这样做。我建议使用splitsql和orderby子句,这样您就可以使用一个具有不同顺序的SQL。