samedi 8 août 2015

Ruby on Rails - Order objects by field of a has_many relation

I'm rails begginer.

how to load database that has following condition.

I want show lecture list that has order by comment.created_at DESC

schema.rb

  create_table "comments", force: :cascade do |t|
    t.text     "content"
    t.integer  "user_id"
    t.integer  "lecture_id"
    t.datetime "created_at",             null: false
    t.datetime "updated_at",             null: false
    t.integer  "likedcount", default: 0
  end

  create_table "lectures", force: :cascade do |t|
    t.string   "subject"
    t.string   "professor"
    t.string   "major"
    t.datetime "created_at",             null: false
    t.datetime "updated_at",             null: false
    t.integer  "uptachi",    default: 0
    t.integer  "hatachi",    default: 0
  end

lecture.rb

has_many :comments

comment.rb

belongs_to: lectures

Aucun commentaire:

Enregistrer un commentaire