【问题标题】:Featured content on index, rails 4 - postgreSQL Heroku索引上的特色内容,rails 4 - postgreSQL Heroku
【发布时间】:2015-01-02 23:30:47
【问题描述】:

我已经设法让帖子成为特色(管理方面),但在索引视图上显示它们时遇到了一些问题。

注意:使用 sqlite 可以正常工作,但在使用 pg 的 heroku 上,不能按预期工作。

注意2:featured_post 是一个字符串(它有“no”或“yes”,默认为“no”)。

我的模特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

我的控制器 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

使用 SQLite,“精选帖子”显示在顶部,无论帖子是否较旧,都按 DESC 排序。所以它按预期工作,一切正常。例如:

  1. 标题一:21.12.2014 精选
  2. 标题二:09.22.2008 精选
  3. 标题三:02.01.2015(今天)
  4. 第四篇:01.01.2015(昨天)

精选位于顶部并按日期排序(最近的精选在前)。

在 Heroku 上使用 PostgreSQL,它给我带来了一些问题。例如:

  1. 标题一:02.01.2015(今天)没有特色(但它显示在顶部)
  2. 标题二:21.12.2014 精选
  3. 标题三:09.22.2008 精选
  4. 第四篇:02.01.2015(今天)
  5. 第五篇:01.01.2015(昨天)

精选 在创建新帖子(默认“无”精选)时不在顶部。如您所见,按日期排序是可以的,但是当创建新帖子时,它“不会”保持在最前面。

不得不说我在heroku postgresql上尝试了DESC和DESC或ASC和ASC或DESC和ASC等,但没有成功。

非常欢迎任何建议和想法。 提前致谢!!

【问题讨论】:

    标签: ruby-on-rails ruby postgresql heroku featured


    【解决方案1】:

    经过半天的搜索并试图让精选帖子保持在最前面并按最近排序,我设法找到了一个解决方案(这不是最好的,但它是一个“临时解决方案”。这是我所做的:

    模型 post.rb

    scope :featured, -> { where(featured_post: 'yes').order(created_at: :desc) } scope :unfeatured, -> { where(featured_post: 'no').order(created_at: :desc) }

    控制器 posts_controller.rb

    @posts_featured = Post.all.featured @posts_unfeatured = Post.all.unfeatured

    查看 index.html.erb

    精选

    <% @posts_featured.each do |post| %> <header><h2 class="title"><%= link_to post.title, html_css_show_path(post) %></h2></header> <div><%= post.body %></div> <% end %>

    没有特色

    <% @posts_unfeatured.each do |post| %> <header><h2 class="title"><%= link_to post.title, html_css_show_path(post) %></h2></header> <div><%= post.body %></div> <% end %>

    这就是诀窍。同样,这不是一个很好的解决方案,但它让我暂时摆脱了这个问题。希望可以帮助别人。顺便说一句,我在 ruby 2.1.4 上使用 rails 4.1.8。干杯!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-04
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-14
      相关资源
      最近更新 更多