首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ho编写一个范围来显示事件的分级- Rails 5

Ho编写一个范围来显示事件的分级- Rails 5
EN

Stack Overflow用户
提问于 2017-01-02 15:53:06
回答 1查看 58关注 0票数 0
  • 我想写一个范围,列出一个事件的所有评级。
  • 谁能建议我如何写一个范围,列出event = Event.find(5)的评级,它表示为:rateable_id => 5

航站楼:

代码语言:javascript
复制
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 

图式

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-02 16:24:40

若要查找给定事件的所有评等,请从Controller调用范围

代码语言:javascript
复制
rates = Rate.find_events(event_id)

和内部的rate.rb文件

代码语言:javascript
复制
scope :find_events, -> (event_id){ where(rateable_id: event_id) }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41429766

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档