2017-01-02 7 views
0
  • イベントのすべてのレーティングを一覧表示するスコープを作成したいと思います。
  • 1は:rateable_id => 5

ターミナルとして表現されevent = Event.find(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'は 'Event.find(5)' – ARTLoe

+0

です。なぜ 'where(rateable_id:id)'スコープ – Abhinay

答えて

1

を特定のイベントのレーティング、コントローラからのコールスコープ

rates = Rate.find_events(event_id) 

rate.rbファイル内

scope :find_events, -> (event_id){ where(rateable_id: event_id) } 
関連する問題