My schema file is set up this way:
ActiveRecord::Schema.define(version: 2022_07_16_173329) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "airlines", force: :cascade do |t|
t.string "name"
t.string "image"
t.string "slogan"
t.string "wlink"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "reviews", force: :cascade do |t|
t.string "image"
t.datetime "date"
t.string "destination"
t.string "seat"
t.string "description"
t.integer "likes"
t.integer "dislikes"
t.bigint "user_id", null: false
t.bigint "airline_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["airline_id"], name: "index_reviews_on_airline_id"
t.index ["user_id"], name: "index_reviews_on_user_id"
end
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.string "admin"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "password_digest"
end
add_foreign_key "reviews", "airlines"
add_foreign_key "reviews", "users"
end
Review is basically the join table How do I find an airline with the most number of reviews? I tried doing something like this but it doesn't work
1).Airline.review.maximum()
2).And return airlines in the ascending order of reviews they contain? Airline.order(:reviews) .....
and 3)Review with the most likes
these methods dont work just gave it a try
CodePudding user response:
Airline.left_joins(:reviews).group(:id).order('COUNT(reviews.id) DESC').first
CodePudding user response:
Review.select(:airline_id).group(:airline_id).order("count(airline_id desc").first.user