如何生成 ActiveRecord 关系并确保在集合中包含特定对象?

How do I generate an ActiveRecord Relation and make sure to include specific objects within the collection?

我想生成如下所示的 ActiveRecord 关系:

@profiles = Profile.published.order("RANDOM()").limit(8) + Profile.where(demo_linked: true)

上面的问题是它 returns 一个数组,这是我不想要的。

我希望以上是一个 ActiveRecord 关系。

想法?

guides所述:

random_published_profiles = Profile.published.order("RANDOM()").limit(8)
demo_linked_profiles = Profile.where(demo_linked: true)

all_profiles = random_published_profiles.merge(demo_linked_profiles)

我最终得到了它:

    @selected_profile = Profile.published.where(demo_linked: true)
    @random_profiles = Profile.published.order("RANDOM()").limit(8).where.not(id: @selected_profile.pluck(:id))
    @profiles = @random_profiles + @selected_profile