【问题标题】:Rails: Select all Children models of Parent and sort by date created in Active RecordRails:选择 Parent 的所有 Children 模型并按 Active Record 中创建的日期排序
【发布时间】:2020-07-18 20:12:20
【问题描述】:

在我的 rails 应用程序中,我试图选择所有引用父模型(“类别”)的子模型(“pdfs”“视频”“信息图表”)。这三个子模型只能属于三个父模型之一(这是使用子模型中的独占弧验证来强制执行的)。所以他们有三分之一的机会属于我的父模型(“类别”)。

有没有办法选择附加到我的特定父模型(“类别”)的所有子模型(“pdfs”“视频”“信息图表”)?如果我能够做到这一点,是否可以按创建日期对子模型的每个实例进行排序?最后,我能否将这个有序的子实例列表作为一个数组,以便在视图中对其进行迭代?

感谢您的帮助!模型设置如下。

父模型:

class Category < ApplicationRecord
  belongs_to :navbar_base_folder
  has_many :sub_categories, dependent: :destroy
  has_many :infographics, dependent: :destroy
  has_many :videos, dependent: :destroy
  has_many :pdfs, dependent: :destroy
end

三个孩子:

class Pdf < ApplicationRecord
  belongs_to :sub_category, optional: true
  belongs_to :category, optional: true
  belongs_to :secret_category, optional: true
  belongs_to :secret_sub_category, optional: true
end

class Video < ApplicationRecord
  belongs_to :sub_category, optional: true
  belongs_to :category, optional: true
  belongs_to :secret_category, optional: true
  belongs_to :secret_sub_category, optional: true
end

class Infographic < ApplicationRecord
  belongs_to :sub_category, optional: true
  belongs_to :category, optional: true
  belongs_to :secret_category, optional: true
  belongs_to :secret_sub_category, optional: true
end

【问题讨论】:

  • 在我看来你需要左连接所有 4 个表然后排序,我会先用 SQL 编写它,然后尝试将它移动到 ActiveRecord。另一种选择只是做 (category.pdfs + category.videos+category.infographics).sort_by(&:created_at)

标签: ruby-on-rails postgresql activerecord model associations


【解决方案1】:

这是单表继承 (STI) 的理想选择。

class Category < ApplicationRecord
  has_many :contents, dependent: :destroy
  has_many :infographics
  has_many :pdfs
  has_many :videos
end

# db/migrate/create_content.rb
# Create a migration for Content
  create_table :content do |t|
    # .... add your attributes here ...
    t.string :type, index: true
  end
end

# app/models/content.rb
class Content < ApplicationRecord
  belongs_to :category
  # `has_many ... optional: true` is not invalid, but you should aim to
  # keep your associations & validations separate. Instead put this as a
  # validation:
  validates :category, presence: true
end

# app/models/pdf.rb
class Pdf < Content; end

# app/models/infographic.rb
class Infographic; end

# app/models/video.rb
class Video < Content; end

然后查找所有内容:

@category = Category.first
@contents = @category.contents.order(date: :desc)

希望有帮助:)

P.S:如果可能,您的 Category 应重构为父/子模式:

class Category < ApplicationRecord
# app/models/category.rb
# Your `Category` table should be using a `parent_id` column, not creating a "sub_category" model.
  belongs_to :parent_category, foreign_key: :parent_id, class_name: 'Category'
  has_many :sub_categories, foreign_key: :parent_id, class_name: 'Category'
  attribute :is_secret, Boolean
end

【讨论】:

    猜你喜欢
    • 2015-09-22
    • 1970-01-01
    • 2010-11-22
    • 1970-01-01
    • 2011-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-28
    相关资源
    最近更新 更多