2017-01-02 4 views
0
  • Я хотел бы написать область, в которой перечислены все рейтинги для события.
  • может один посоветовать мне, как написать объем этого списка рейтингов для event = Event.find(5), который представлен как :rateable_id => 5

Терминала:Хо написать область для отображения оценок для события - Rails 5

2.3.0 :005 > event = Event.find(5) 
    Event Load (0.2ms) SELECT "events".* FROM "events" WHERE "events"."id" = ? LIMIT ? [["id", 5], ["LIMIT", 1]] 
=> #<Event id: 5, title: "Speed Social - Graduate Professionals", description: "Lorem ipsum dolor sit amet, consectetur adipiscing...", created_at: "2016-12-04 14:02:09", updated_at: "2016-12-04 14:02:09", slug: nil> 
2.3.0 :006 > 
2.3.0 :007 > 
2.3.0 :008 > ap Rate.all 
    Rate Load (0.3ms) SELECT "rates".* FROM "rates" 
[ 
    [0] #<Rate:0x007f84fd142380> { 
        :id => 8, 
      :rater_id => 1, 
     :rateable_type => "Event", 
      :rateable_id => 5, 
       :stars => 3.0, 
      :dimension => "style", 
      :created_at => Mon, 02 Jan 2017 15:00:51 UTC +00:00, 
      :updated_at => Mon, 02 Jan 2017 15:00:51 UTC +00:00 
    }, 
    [1] #<Rate:0x007f84fd142178> { 
        :id => 9, 
      :rater_id => 4, 
     :rateable_type => "Event", 
      :rateable_id => 5, 
       :stars => 2.0, 
      :dimension => "style", 
      :created_at => Mon, 02 Jan 2017 15:12:29 UTC +00:00, 
      :updated_at => Mon, 02 Jan 2017 15:12:29 UTC +00:00 
    }, 
    [2] #<Rate:0x007f84fd141f70> { 
        :id => 10, 
      :rater_id => 1, 
     :rateable_type => "Event", 
      :rateable_id => 6, 
       :stars => 4.0, 
      :dimension => "style", 
      :created_at => Mon, 02 Jan 2017 15:40:37 UTC +00:00, 
      :updated_at => Mon, 02 Jan 2017 15:40:37 UTC +00:00 
    } 
] 
=> nil 

схемы

ActiveRecord::Schema.define(version: 20170102134239) do 

    create_table "events", force: :cascade do |t| 
    t.string "title" 
    t.text  "description" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    t.string "slug" 
    t.index ["slug"], name: "index_events_on_slug", unique: true 
    end 

    create_table "events_users", id: false, force: :cascade do |t| 
    t.integer "event_id", null: false 
    t.integer "user_id", null: false 
    end 

    create_table "overall_averages", force: :cascade do |t| 
    t.string "rateable_type" 
    t.integer "rateable_id" 
    t.float "overall_avg", null: false 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    create_table "rates", force: :cascade do |t| 
    t.integer "rater_id" 
    t.string "rateable_type" 
    t.integer "rateable_id" 
    t.float "stars",   null: false 
    t.string "dimension" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.index ["rateable_id", "rateable_type"], name: "index_rates_on_rateable_id_and_rateable_type" 
    t.index ["rater_id"], name: "index_rates_on_rater_id" 
    end 

    create_table "rating_caches", force: :cascade do |t| 
    t.string "cacheable_type" 
    t.integer "cacheable_id" 
    t.float "avg",   null: false 
    t.integer "qty",   null: false 
    t.string "dimension" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.index ["cacheable_id", "cacheable_type"], name: "index_rating_caches_on_cacheable_id_and_cacheable_type" 
    end 

    create_table "users", force: :cascade do |t| 
    t.string "email",     default: "", null: false 
    t.string "encrypted_password",  default: "", null: false 
    t.string "reset_password_token" 
    t.datetime "reset_password_sent_at" 
    t.datetime "remember_created_at" 
    t.integer "sign_in_count",   default: 0, null: false 
    t.datetime "current_sign_in_at" 
    t.datetime "last_sign_in_at" 
    t.string "current_sign_in_ip" 
    t.string "last_sign_in_ip" 
    t.datetime "created_at",       null: false 
    t.datetime "updated_at",       null: false 
    t.string "firstname" 
    t.string "lastname" 
    t.string "slug" 
    t.index ["email"], name: "index_users_on_email", unique: true 
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true 
    t.index ["slug"], name: "index_users_on_slug", unique: true 
    end 

end 
+0

Вы используете любой драгоценный камень для рейтинга? если нет, тогда я могу узнать, что такое 'rateable_id' – Abhinay

+0

' rateable_id' - объект, который оценивается. Это могут быть события, пользователь, комментарий и т. Д. Я использую 'gem ratyrate' -' rateable_id: 5' is 'Event.find (5)' – ARTLoe

+0

Почему вы не можете просто написать что-то вроде 'where (rateable_id: id)' в модели 'rate.rb' для области – Abhinay

ответ

1

Чтобы найти все Рейтинги для данного события, объем вызовов от Контролера

rates = Rate.find_events(event_id) 

и внутри rate.rb файла

scope :find_events, -> (event_id){ where(rateable_id: event_id) } 
Смежные вопросы