'order clause'中的ROR mysql- unknown列

时间:2015-07-18 10:12:24

标签: mysql sql ruby-on-rails

我有一个包含组列表的页面:

gtoups / index.html.haml

%h1= title (@user ? link_to(@user,@user)+" / groups" : "Groups")

= render "groups/top_links"

= render 'search_form'

- if @groups.nil?
  %p Text
- else
  = wp @groups
  %ul.unstyled.groups= render :partial => 'groups/group', :collection => @groups
  = wp @groups

当我访问详细信息组时,会显示错误:

Mysql2::Error: Unknown column 'posts.last_comment_at' in 'order clause': 
SELECT  DISTINCT `posts`.id FROM `posts` LEFT OUTER JOIN `groups` ON
`groups`.`id` = `posts`.`group_id` LEFT OUTER JOIN `memberships` ON
`memberships`.`group_id` = `groups`.`id` WHERE `posts`.`group_id` = 489 AND
(posts.user_id NOT IN(0)) AND (groups.closed=0 || (groups.closed=1 AND
memberships.user_id=30453)) AND ((`groups`.`deleted` = 0 AND
`posts`.`deleted` = 0)) AND (posts.draft=0 OR posts.user_id=30453) ORDER BY
last_comment_at DESC LIMIT 10 OFFSET 0

我的方法 show in groupscontroller.rb:

def show
    order = case params['order']
            when 'count'
              @order = 'count'
              'posts.comments_count DESC'
            #else
            #  @order = 'time'
            #  'posts.updated_at DESC'
            else
              @order = 'time'
              'posts.last_comment_at DESC'
            end

    #@posts = @group.posts.where("posts.user_id NOT IN(?)", mutes).visible(current_user).order(order).page(params[:page]).per_page(10)
    @posts = @group.posts.where("posts.user_id NOT IN(?)", Mute.muteable_ids_users_by(current_user)).visible(current_user).order(order).page(params[:page]).per_page(10)
  end

schema.rb

.....................................................................
create_table "posts", :force => true do |t|
    t.integer  "user_id",                              :null => false
    t.integer  "group_id",                             :null => false
    t.string   "subject",                              :null => false
    t.text     "body",                                 :null => false
    t.datetime "created_at",                           :null => false
    t.datetime "updated_at",                           :null => false
    t.integer  "comments_count",    :default => 0,     :null => false
    t.boolean  "deleted",           :default => false, :null => false
    t.datetime "deleted_at"
    t.integer  "deleted_by_id"
    t.boolean  "delta",             :default => true,  :null => false
    t.string   "tags_line"
    t.string   "category"
    t.boolean  "comments_disabled", :default => false, :null => false
    t.boolean  "draft",             :default => false, :null => false
    t.datetime "published_at"
    t.datetime "last_comment_at"
  end
........................................................................

groupdecorator.rb

# encoding: utf-8
class GroupDecorator < Draper::Base
  decorates :group
  decorates_association :user
  decorates_association :owner

  def title
    if model.deleted?
      h.content_tag :span, title: "Группа удалена", class: 'deleted tTip' do
        model.title
      end
    else
      model.title
    end
  end

  def to_s
    model.title
  end

  def rating
    h.content_tag :sup, model.rating, class: 'rating', title: model.rating
  end

  def list_avatar
    h.content_tag :div, class: 'avatar' do
      h.link_to h.image_tag(model.avatar.url(:thumb), alt: model.to_s, hidpi_src: model.avatar.url(:'thumb@2x'), size: '100x100'), model
    end
  end

  def grouppic(size = :medium)
    h.image_tag(model.avatar.url(size), alt: model.to_s)
  end
end

为什么错误未知列'last_comment_at'... 显示?如何查看错误以及如何解决?

1 个答案:

答案 0 :(得分:0)

再次重新迁移。该列已添加到表中。我不知道,为什么第一次迁移还没有通过。现在一切都很好。