I had managed to allow posts to be featured (admin side), but I had some problems displaying them on the index view.
Note: with sqlite works fine, but on heroku with pg, doesn't work as expected.
Note2: featured_post is a string (it has "no" or "yes", "no" by default).
My model post.rb
if Rails.env.production?
scope :featured, -> { order(featured_post: :asc, created_at: :desc).limit(100) } #pg
else
scope :featured, -> { order(featured_post: :desc, created_at: :desc).limit(100) } #sqlite
end
My controller posts_controller.rb
def index
if params[:search]
@posts = Post.search(params[:search]).order("created_at DESC").paginate(page: params[:page], :per_page => 20)
else
@posts = Post.all.featured
end
end
With SQLite the "featured posts" are shown on top, and ordered by DESC no matter if the post is older. So it works as expected, everything OK. For example:
- Title One : 21.12.2014 featured
- Title Two : 09.22.2008 featured
- Title three : 02.01.2015 (today)
- Title four : 01.01.2015 (yesterday)
featured is on top and ordered by date (recent featured first).
With PostgreSQL on Heroku, it gives me some problems. For example:
- Title One : 02.01.2015 (today) not featured (but it show on top)
- Title Two : 21.12.2014 featured
- Title Three : 09.22.2008 featured
- Title Four : 02.01.2015 (today)
- Title Five : 01.01.2015 (yesterday)
featured is not on top when new post (default "no" featured) is created. The ordering by date is ok, as you see, but it "doesn't" stay on top, when a new post is created.
Had to say that I tried DESC and DESC or ASC and ASC or DESC and ASC, etc on heroku postgresql, with no success.
Any suggestions, ideas are much welcomed. Thanks in advance!!