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.
This is what I have
Post model
:
class Post < ActiveRecord::Base
belongs_to :categories
scope :by_category, ->(category) { joins(categories).where(categories: { id: category.id }) }
scope :enabled, where(disabled: false)
scope :recent, order('published_at DESC')
Category model
class Category < ActiveRecord::Base
has_many :feeds
scope :most_popular, order('rank ASC')
Home Controller
def index
Category.most_popular.limit(20).each do |cat|
@posts= Post.enabled.recent.by_category(cat).page(1).per(30)
end
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 << to append.. as in -
@posts << Post.enabled.recent.by_category(cat).page(1).per(30)
But it gives no method << for nil:nil class
I tried making @posts as an array, but then it does not take page and per of kaminari into play.
I tried making @posts as ActiveRecord::Relation object using new, it gave argument error.
I tried making @posts as object of Post but then it says undefined method << for Post, since ofcourse, << is not a method for my model class. I also followed some SO posts but it didn't seem to fit in my step.
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.