title: append to an active record relations object
body: <p>I have a Post model. Having a scope which finds the enabled posts.A post can belong to a category. I also have a Category model, with categories ranked. Category has many posts. I want to display first distinct 20 posts which belong to distinct categories(which are the top 20 categories) and then display the rest of the post in descending order of their published_time.</p>
  
  <p>This is what I have</p>
  
  <p><code>Post model</code>:</p>
  
  <pre><code>class Post &lt; ActiveRecord::Base
  belongs_to :categories
  scope :by_category, -&gt;(category) { joins(categories).where(categories: { id: category.id }) }
  scope :enabled, where(disabled: false)
  scope :recent, order('published_at DESC')
  </code></pre>
  
  <p><code>Category model</code></p>
  
  <pre><code>class Category &lt; ActiveRecord::Base
  has_many :feeds
  scope :most_popular, order('rank ASC')
  </code></pre>
  
  <p><code>Home Controller</code></p>
  
  <pre><code>def index
    Category.most_popular.limit(20).each do |cat|
      @posts= Post.enabled.recent.by_category(cat).page(1).per(30)
    end
  </code></pre>
  
  <p>in view file I am rendering the attributes of the post that i receive using @posts. But as obvious, it returns only the post of the last category found in the loop. Basically it doesn't append. I tried using &lt;&lt; to append.. as in - </p>
  
  <pre><code>@posts &lt;&lt;  Post.enabled.recent.by_category(cat).page(1).per(30)
  </code></pre>
  
  <p>But it gives no method &lt;&lt; for nil:nil class</p>
  
  <p>I tried making @posts as an array, but then it does not take page and per of kaminari into play.</p>
  
  <p>I tried making @posts as ActiveRecord::Relation object using new, it gave argument error. </p>
  
  <p>I tried making @posts as object of Post but then it says undefined method &lt;&lt; for Post, since ofcourse, &lt;&lt; is not a method for my model class. I also followed some <a href="https://stackoverflow.com/questions/4693900/ruby-rails-add-records-to-an-object-with-each-loop-iteration-object-vs-array">SO posts</a> but it didn't seem to fit in my step.</p>
  
  <p>Basically, my insight into achieving this was appending records into the model object and then displaying the object. I even doubt, if my approach is good enough. There could be more efficient way of doing this, which I may be missing out in RoR. </p>