Railsで単一カラムのテーブルを並べ替えることができません。各行は単一のオブジェクト(記事)を表し、すべての属性(name
,content
,created_at
,user
など)を含みます。検索機能は正常に動作しますが(Article.where
)、表を任意の属性、つまりArticle.order('attribute')
で並べ替えることはできません。私が変更できないデフォルトはcreated_at desc
です。私は何か見落としていますか?テーブルをレールで並べ替えることができません
def index
@title="Home"
if params[:search]
@search=params[:search]
@articles=Article.where('name LIKE ? OR category LIKE ?', "%#{params[:search]}%", "%#{params[:search]}%").paginate(:per_page => 15, :page => params[:page])
else
@articles=Article.order('name').paginate(:per_page => 15, :page => params[:page])
end
end
とビュー:
<table>
<%= render @articles%>
</table>
<%= will_paginate @articles, :previous_label => "Prev", :next_label => "Next" %>
OT:名前付きプレースホルダを使って 'where'を少し良くし、LIKEは常に大文字と小文字を区別するわけではありませんので、すべてをダッシュダウンすることができます:' .where( 'lower(name)like:pat or lower pat '、:pat => "%#{params [:search] .downcase}%") 'のように、 –
ありがとう..私はあなたの提案を取り入れます –