【问题标题】:Active Admin - custom order/sorting with position in has_many to has_many polymorphicActive Admin - 自定义顺序/排序,在 has_many 到 has_many 多态中的位置
【发布时间】:2020-04-01 23:42:52
【问题描述】:

我有一个由 Active Admin 管理的多态 has_many 到 has_many 关联。 有两种不同的模型 BlogArticle 和 MagazineArticles 通过 Authoring 表有许多作者。 关联工作正常,但我必须在活动管理员中创建某种输入,让管理员为任何一篇文章指定作者的出现顺序。

因此,目标是获取任何 BlogArticle 和 Magazine Article 都有许多作者及其自定义顺序。

我可以在创作表中使用位置属性,但我们如何在 Active Admin 中管理它?

此时我在 MagazineArticle 和 BlogArticle 中的输入字段是:

f.input :authors, 
   label: "Autori",
   as: :select, multiple: true,
   collection: Author.all.collect {|item| [item.full_name, item.id] }
end

此“选择”将作者按作者对象(不是创作对象)的 updated_at 排序。此选择来自一个名为 activeadmin-addons 的 gem,它对我们来说是完美的,但我也可以接受另一种继续方式。

型号是:

class Authoring < ApplicationRecord
    belongs_to :author
    belongs_to :authorable, :polymorphic => true
    default_scope {order(position: :asc)}
end

class Author < ApplicationRecord
    has_many :authorings
    has_many :blog_articles, :through => :authorings,
        :source => :authorable,
        :source_type => "BlogArticle"
    has_many :magazine_articles, :through => :authorings,
        :source => :authorable,
        :source_type => "MagazineArticle"
end

class BlogArticle < ApplicationRecord
    has_many :authorings, :as => :authorable, dependent: :destroy
    has_many :authors, :through => :authorings 
end

class MagazineArticle  < ApplicationRecord
    has_many :authorings, :as => :authorable, dependent: :destroy
    has_many :authors, :through => :authorings
end

它正确地添加了我需要的所有作者,但显然没有任何排序。

如何在 Active Admin 中管理创作者的位置?

------- 编辑 --------

我已按照@Piers C(非常感谢)的建议尝试使用似乎符合我需求的activeadmin_select_many gem,但它现在与我的 active_admin 版本不兼容

Bundler could not find compatible versions for gem "activeadmin":
  In snapshot (Gemfile.lock):
    activeadmin (= 2.6.1)

  In Gemfile:
    activeadmin

    activeadmin_select_many was resolved to 0.3.4, which depends on
      activeadmin (~> 1.0)


我尝试了“捆绑更新”但无事可做 - 存在大量不兼容问题

Bundler could not find compatible versions for gem "activeadmin":
  In Gemfile:
    activeadmin

    activeadmin_select_many was resolved to 0.3.4, which depends on
      activeadmin (~> 1.0)

Bundler could not find compatible versions for gem "activemodel":
  In Gemfile:
    carrierwave (~> 2.0) was resolved to 2.1.0, which depends on
      activemodel (>= 5.0.0)

    rails (~> 6.0.0) was resolved to 6.0.1.rc1, which depends on
      activemodel (= 6.0.1.rc1)

    web-console (>= 3.3.0) was resolved to 4.0.1, which depends on
      activemodel (>= 6.0.0)

Bundler could not find compatible versions for gem "activerecord":
  In Gemfile:
    friendly_id was resolved to 5.3.0, which depends on
      activerecord (>= 4.0.0)

    rails (~> 6.0.0) was resolved to 6.0.1.rc1, which depends on
      activerecord (= 6.0.1.rc1)

Bundler could not find compatible versions for gem "activesupport":
  In Gemfile:
    bullet was resolved to 6.1.0, which depends on
      activesupport (>= 3.0.0)

    jbuilder (~> 2.7) was resolved to 2.10.0, which depends on
      activesupport (>= 5.0.0)

    rails (~> 6.0.0) was resolved to 6.0.1.rc1, which depends on
      activesupport (= 6.0.1.rc1)

    rename was resolved to 1.0.6, which depends on
      activesupport

    webpacker (~> 4.0) was resolved to 4.2.2, which depends on
      activesupport (>= 4.2)

Bundler could not find compatible versions for gem "listen":
  In Gemfile:
    listen (>= 3.0.5, < 3.2)

    spring-watcher-listen (~> 2.0.0) was resolved to 2.0.1, which depends on
      listen (>= 2.7, < 4.0)

Bundler could not find compatible versions for gem "rails":
  In Gemfile:
    rails (~> 6.0.0)

    rename was resolved to 1.0.6, which depends on
      rails (>= 3.0.0)

Bundler could not find compatible versions for gem "railties":
  In Gemfile:
    activeadmin was resolved to 1.2.1, which depends on
      railties (>= 4.2, < 5.2)

    activeadmin_addons was resolved to 1.7.1, which depends on
      railties

    devise was resolved to 4.7.1, which depends on
      railties (>= 4.1.0)

    dotenv-rails was resolved to 2.7.5, which depends on
      railties (>= 3.2, < 6.1)

    rails (~> 6.0.0) was resolved to 6.0.1.rc1, which depends on
      railties (= 6.0.1.rc1)

    rails-i18n was resolved to 6.0.0, which depends on
      railties (>= 6.0.0, < 7)

    sass-rails (~> 5) was resolved to 5.1.0, which depends on
      railties (>= 5.2.0)

    web-console (>= 3.3.0) was resolved to 4.0.1, which depends on
      railties (>= 6.0.0)

    webpacker (~> 4.0) was resolved to 4.2.2, which depends on
      railties (>= 4.2)

【问题讨论】:

  • 你看过activeadmin_select_many吗?
  • 哇,看起来棒极了,我从未见过,但有一个非常大的问题:gem 与我的活动管理员版本不兼容,我在下面编辑我的问题
  • 我会联系 Mattia 并询问他这些天在做什么,询问他是否愿意帮助更新。

标签: ruby-on-rails sorting many-to-many activeadmin polymorphic-associations


【解决方案1】:

根据我对你的问题的理解,

你想要的结果是你打电话的时候 @blog_article.authors@magazine_article.authors,它应该按照 Author 的 'position' 属性排序的顺序提供该文章的所有作者。

您可以通过在BlogArticleMagazineArticle 模型中添加has_many :authors 关联来实现此目的。

您的关联将如下所示:

has_many :authors, -> {order('authors.position')}, :through => :authorings

您可以在 Author 模型中定义默认范围,如下所示:

default_scope { order('position desc')}

【讨论】:

  • 谢谢 Sohail,但不,您的解决方案很简单 - 我需要每篇文章的不同顺序。所以放置位置的正确位置是创作(并使用默认范围)。但我的问题有点复杂,如何使用 active admin 管理每篇文章中作者的位置?
猜你喜欢
  • 1970-01-01
  • 2011-05-11
  • 1970-01-01
  • 2012-05-16
  • 1970-01-01
  • 1970-01-01
  • 2016-07-13
  • 1970-01-01
  • 2013-05-02
相关资源
最近更新 更多