0

How to add pagination to feed_entries in category?

I use kaminari, mongoid 4, rails 4

Category.rb

class Category
  def feed_entries
    FeedEntry.in(source_id: sources.map(&:id))
  end
end

show.html.erb

<% @category.feed_entries.includes(:source).each do |feed_entry| %>
  <%= link_to feed_entry.name, feed_entry %>
  <%= feed_entry.source.title %>
<% end %>

Models

class Category
  include Mongoid::Document
  field :name, type: String
  has_many :sources, dependent: :destroy
end

class FeedEntry
  include Mongoid::Document
  field :name, type: String
  belongs_to :source, touch: true
  validates :source_id, presence: true
end

class Source
  include Mongoid::Document
  field :title, type: String
  has_many :feed_entries, dependent: :destroy
  belongs_to :category, touch: true
end
Anton Ipatov
  • 169
  • 2
  • 14

1 Answers1

0

Something like this should work

in you categories controller

 def show 
     @category = Category.find(params[:id]) 
     @feed_entries = @category.sources.includes(:feed_entries).page(params[:page])
 end

In view

<%= @feed_entries.each do |feed_entry| %>
  <%= link_to feed_entry.name, feed_entry %>
  <%= feed_entry.source.title %>
<% end %>
<%= paginate @feed_entries %>
Sonalkumar sute
  • 2,565
  • 2
  • 17
  • 27