実現したいこと
ここに実現したいことを箇条書きで書いてください。
- date型カラムに登録した順に上部に最新のものを表示、下部に以降のページを並べて表示したいです。
- createされた日付順ではなく、日付カラム(date型)の最新日順で表示させたいです。
<まとめ>
・date順に並び替え
・dateの最新データの表示
・dateの最新の2つ目以降の一覧表示
前提
画像つきの日記アプリを作成しております。
基本的には、当日の日記記入になると思いますが、イレギュラーで2日前のものを記入する場合に2日前の記事が最新になってしまいます。(現在は登録した日付順ではなく、記入した順に表示されています。)
該当箇所
Controller
1def index 2 @notes = Note.all 3 end
該当のソースコード
ruby
1class CreateNotes < ActiveRecord::Migration[6.0] 2 def change 3 create_table :notes do |t| 4 t.date :record_date, null: false 5 t.integer :weather_id, null: false 6 t.string :responsible_person, null: false 7 t.time :utilization_time, null: false 8 t.integer :body_temperature 9 t.integer :pulse 10 t.integer :blood_pressure 11 t.integer :taking_medicine_id, null: false 12 t.integer :usage_type_id, null: false 13 t.integer :bathing_id, null: false 14 t.text :diary, null: false 15 t.references :user, null: false, foreign_key: true 16 17 t.timestamps 18 end 19 end 20end
ruby
1<% @notes.each do |note| %> 2 <li class='list'> 3 <%= link_to note_path(note.id) do %> 4 <div class='note-img-contents'> 5 <div class='note-img-content'> 6 <%= image_tag note.image, class: "note-img" %> 7 <p><%=note.record_date.month %>/<%=note.record_date.day %></p> 8 </div> 9 </div> 10 <% end %> 11 </li> 12 <% end %>
試したこと
・コントローラーに(date: :desc)と('created_at DESC')を試してみたが記入した日付順になってしまう。
@notes = Note.all('record_date DESC')にするとエラーになる。
以下エラー文
ArgumentError in NotesController#index
wrong number of arguments (given 1, expected 0)
・viewにeachを使用して繰り返し表示することはできた。
お手数ですがご教示いただけると幸いです。

回答2件
あなたの回答
tips
プレビュー